[mapserver-users] RE: javascript in html templates

Lime, Steve D (DNR) Steve.Lime at state.mn.us
Wed Jan 25 18:16:11 EST 2012


Yes, the MapServer-processed template is the AJAX response.

If you display the AJAX content in a new window (e.g. window.open(...)) or frame then javascript in the content will be executed because it's essentially a new web page. The problem occurs when you want to display the AJAX content in an existing page, say in a new or existing div. The browser won't execute the javascript in this case.

I believe you need to extract the scripts from within the content and execute them yourself. One way to do that would be to load the html into the div and then do something like:

  // see if there are any scripts in the response to execute
  var popup = document.getElementById("popupDiv");
  var scripts = popup.getElementsByTagName("script");
  for(var i=0;i<scripts.length;i++) eval(scripts[i].text);

I found this on the web someplace awhile back. There may (and likely are) more elegant solutions but this can work. I use it here:

  http://www.dnr.state.mn.us/watertrails/interactive_map/index.html?mapext=407059.5703125+5165944.8242188+418940.4296875+5172755.3710938

If you click on one of the black recreation icons that returns template output that contains javascript. It's executed with the code above.

Steve

-----Original Message-----
From: mapserver-users-bounces at lists.osgeo.org [mailto:mapserver-users-bounces at lists.osgeo.org] On Behalf Of KenHeer1
Sent: Wednesday, January 25, 2012 11:50 AM
To: mapserver-users at lists.osgeo.org
Subject: [mapserver-users] RE: javascript in html templates

I hope you might be able to help me out a little further, I am still a little
new to this.

I'm using Openlayers to perform my getfeatureinfo request. The Mapserver
Template file that is returned *is* my ajax response, right?

That response is a full html page with the javascript in it. If I understand
this right, nothing in a <script> tag will execute on this page, is that
also correct?

I am vagely familiar with jQuery and ExtJs but everything I've looked at
seems to require involvement when the initial request is sent, not just
handling the html response that's returned. Even when I incorporate a simple
jQuery alert into the response page, it's still in the script tag and
doesn't get fired. It seems like my problem starts because I'm feeding the
completed mapserver template html file straight to the openlayers function
and then using openlayers.loadURL and the openlayers popups.

Instead of using a popup, if I posted the response to a div on the original
page I believe I'd be able to use some of the examples that I found online
to handle the new, ajax delivered content. But since it's a popup that's
essentially a separate html page, that won't work, right?




Lime, Steve D (DNR) wrote
> 
> The answer here depends on your development framework. It's not a
> MapServer issue. By default the browser won't execute javascript in the
> response and you have to work around that. Frameworks like ExtJS provide
> options to do this via their AJAX classes. Check your framework's (e.g.
> jQuery, Dojo or whatever) docs for more information. There are also
> solutions on the web, search for something like "executing javascript in
> ajax response".
> 
> Steve
> 
> 


--
View this message in context: http://osgeo-org.1560.n6.nabble.com/javascript-in-html-templates-tp4336706p4338841.html
Sent from the Mapserver - User mailing list archive at Nabble.com.
_______________________________________________
mapserver-users mailing list
mapserver-users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/mapserver-users




More information about the mapserver-users mailing list