开发者

Reference service-side code from <script> tag

开发者 https://www.devze.com 2023-01-23 03:03 出处:网络
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\"></script>

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"></script> 

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 开发者_如何学Gowhere:

  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'.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消