views:

28

answers:

1

I don't know where I got the idea before but a year ago I wrote this in php:

<script type="text/javascript" src="http://www.mydomain.com/getmarkers.php"&gt;&lt;/script&gt; 

Now I'm ready to convert this website to an ASP.NET MVC website and I'm wondering what the best way is to convert this into something more 'normal?'.

The options I could think about where:

  1. Custom HttpHandler for .js files
  2. Keep using the script tag but with a custom route to an action
  3. Modify the javascript to load the serverside data using an ajax call

What the getmarkers.php currently does is generate javascript to add markers to a google map. The benefit of referencing the php inside the script tag is that

  1. It's never cached, the markers are always up to date (I know there are alternatives)
  2. It keeps my html clear of any javascript
  3. Very easy to add/remove certain fields for the generated markers

An example of what is being generated :

infoWindows[0] = new google.maps.InfoWindow({
    content: '<div style="width:250px;color:#000;">...html content for this specific marker...</div>'
});

google.maps.event.addListener(markers[0], 'click', function() {
    infoWindows[0].open(map, markers[0]);
});

What changes is the index (0 in this example) and the content of the html.

Question
1. What solution do you think fits best
2. Is it bad to reference a script by calling a serverside 'file'.

+1  A: 

I can see no downside to embedding server-generated .js files. I personally would choose the "custom route" option so it's clear it's a generated file, leaving the .js extension to static resources.

However, serving the markers in a pure data format like JSON, and loading them using Ajax would have the advantages that

  • you have the data in a neutral "meta format" that you can re-use elsewhere without having to build a new data source

  • you can keep the Loading / HTML generating process in one place, the parent page or one of its static scripts, instead of controlling the way the HTML looks in the server-side script

  • the amount of transferred data is probably reduced

  • It's cleaner overall

Pekka
+1 for the tip "so it's clear it's a generated file, leaving the .js extension to static resources". My preference is leaning towards a json approach too using ajax to get the json. It would just take longer for the map to generate as the content will be loaded AFTER the body is loaded, now it loads before.
Peter
@Peter true, that's a good point. There is bound to be a noticeable flicker when using Ajax
Pekka