Integrating external JavaScript files with ajax in Ruby on Rails

After creating a Rails module that generates JavaScript for a Google map, I encountered an issue. Whenever the user modifies the webpage, the observe_field function is used to send a callback to the server in order to update the map's JavaScript without refreshing the entire page. However, I am struggling to find a suitable method for inserting this new JavaScript into the page.

<div id='my_div_1'>div1</div>
<%= 
  update_page_tag do |page|
    page.replace_html 'my_div_1', "<script>alert('hi');</script>"
  end
%>

It appears that using replace_html only functions properly with non-script HTML content. When attempting to include the closing </script> tag, it encounters errors.

The following link leads to a page that may provide insights on this issue:

Answer №1

One approach I experimented with that showed some promise was implementing an observe_field on the page and using prototype's Ajax.Updater to fetch the necessary JavaScript for the map functionality. The success rate of this method is around 95%, although there is a lingering doubt about its reliability in the remaining 5%. Upon downloading and running the JavaScript, a global init_map function is invoked, which was included in the downloaded content.

<% observe_field(:id_of_field_to_watch, 
:function => "
new Ajax.Updater('map_id', '/filter/map',   
{ 
    evalscripts: true,
    onComplete: function() { init_map() }
}); 
")
%>

Answer №2

Instead of relying on dynamic javascript, I decided to switch things up and use a json data structure to power the map. Now, whenever the map markers are updated, I simply download the necessary json data using Rails replace_html method and seamlessly integrate them into the map.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Enhance your Material-UI ListItems with dynamic ripple colors when using React-Router NavLink

Currently, I am attempting to nest a Material-UI <ListItem button> within a react-router <NavLink>. While functionality is present, I have observed that the ripple colors on the <ListItem button> are being altered by the <NavLink> C ...

React state not being properly updated

Upon calling the verifyPassword function and attempting to update the user state using setUser, I encounter an issue where the state is not being updated. Consequently, the values of the input fields remain unchanged and even when logging the user object ...

The Controller of Conversations

In the HTML code, there is a dialog element with an identification of divMyDialog1. This dialog is connected to a JavaScript class called MyDialog1. Each dialog has its own unique id and associated class name. MyDialog1 = function(divStr){ this.divStr ...

Can values from a dgrid store be saved in a variable or displayed in the console?

Currently, I am utilizing the dgrid store to display a grid (dgrid 0.4) on my website. Below is the code snippet that I have implemented: require([ 'dojo/_base/declare', 'dojo/Deferred', 'dstore/RequestMemory', ...

Updating the display text length on vue-moment

Currently, I am attempting to showcase an array of numbers const days = [1, 7, 14, 30, 60] in a more human-readable format using vue-moment Everything is functioning correctly {{ days | duration('humanize') }} // 'a day' // '7 d ...

The Protractor option is nowhere to be found on the Run Configuration popup window in Eclipse

Issue with Protractor in Eclipse: Unable to locate Protractor option on Run Configuration popup. Despite following the steps outlined in http://www.protractortest.org/#/ and this guide on configuring Protractor with Eclipse (specifically the 2 Answer step ...

searching for a refined method to tackle this challenge

I'm relatively new to Angular and I'm interested in finding a more efficient solution for this particular task. The code below functions correctly, but I am uncertain if it is the best approach to achieve the desired outcome. HTML <div ng-a ...

What is the best way to continuously execute the ajax request for downloading a pdf file?

if( $.isArray(certTypeReq) ){ $.each(certTypeReq,function(key,certType){ $.ajax({ url: baseUrl+"/user/certificate/getlink", type: 'GET', data: { 'certType' : certType}, ...

Object Illumination Effect Using Three.js

Currently, I am working on creating an earth scene using Three.js and everything seems to be going well except for the large and distracting reflection disc that appears on the surface of the earth object. Does anyone have any suggestions on how to either ...

Syntax error encountered while parsing JSON data (jQuery version 1.7.2)

My current challenge involves extracting data from a JSON object located on a different page within my website. This particular page is hosted on an ecommerce platform, which limits my access to server side controls and certain elements. I have encountere ...

Ways to verify the uniqueness of usernames within Firebase

I have been working on a function to check if a username is unique in my Firebase database. Despite researching and implementing code, it doesn't seem to work as expected. Any assistance would be greatly appreciated - what am I missing? onChangeUser ...

Are Global variables supported in the EXT JS framework?

When working with Java and C++, it is common practice to store a variable globally so that its value can be accessed from anywhere within the project. For example, if I am working inside a class named Residence and saving an INT as the residenceNumber to a ...

The issue with the smooth scrolling feature in next/link has not been resolved

I am currently facing an issue where smooth scrolling is not working when using next/Link, but it works perfectly fine with anchor tags. However, the downside of using anchor tags is that the page reloads each time, whereas next/link does not reload the pa ...

What is causing Bxslider to malfunction?

I need help troubleshooting an issue with my HTML code. The slideshow I'm trying to create isn't working as expected; all images are displaying vertically and I'm getting an error message saying that bxslider() is not a function. Can anyone ...

Mysterious anomaly observed: Peculiar trajectory detected in canvas image during left

I have a fascinating HTML5 Canvas that showcases the movement of two identical objects to both the right and the left. Although the right side operates flawlessly, I've noticed that the left object leaves behind an intriguing trail with a hint of gree ...

The protocol at msa port 587 is unidentified with SSL23_GET_SERVER_HELLO

For sending email notifications when a new user registers or forgets their password, I am currently working on a Linux environment with an app built using node.js. Error: [Error: 140020013401920:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown ...

Is there a conventional method for implementing role-based access control for files in Angular?

I have built 4 projects in Angular that grant access to different roles. The dashboard consists of various content pages, with the initial page being the dashboard when a user logs in. The content displayed on the dashboard is determined by the logged-in ...

The Backbone.js template does not automatically inherit attributes from the model

Hey there, I'm currently working on setting up a simple SPA using Backbone for testing purposes. However, when I try to render my view, the Underscore template doesn't seem to be picking up any attributes from my model. I've been trying to t ...

Integrating a real-time chat feature on a website

Considering adding a live chat support feature to my website specifically for new users seeking information about my services. I've been contemplating the most effective way to incorporate this solution without relying on third-party options. My idea ...

ES5 enables the extension of classes in React

This ES6 syntax works fine for me: import {Component} from 'react'; class A extends Component {} class B extends A { // I can redeclare some methods here } But how would one implement this using ES5? Like so: var React = require('reac ...