开发者

Passing variables to JavaScript inside a URL?

I'm dealing with a web-service that serves video files (host). They provide you with a Guid which is used in an embed code (link to js file).

Their URL's are structured like this:

http://开发者_运维知识库www.foo.com/34534525lsjda345435/script.js

Somehow, the script.js parses this URL and retrieves the variable. Can someone clue me in how this is done?

What kind of script runs on the server-side to respond to these requests? After all, the string in the URL doesn't really refer to a real directory, or does it?

Any help/insights would be appreciated.


The server could be doing URL Rewriting. A rewrite engine would convert that URL to point to some server side script that returns dynamically generated JavaScript. The rewritten URL could look something like this:

http://www.foo.com/some-server-side-script.php?id=34534525lsjda345435


They most likely rewrite the URL and pass that ID to a script which generates the Javascript -- by generate I mean embed any video-specific data into the javascript. A javascript file doesn't have knowledge of the URL where it resides, only the URL of the window or frame where it's being loaded.

foo.com/$1/script.js -> foo.com/gen_script?id=$1 

Where foo.com/gen_script?id=$1 is the script that actually generates that file.

If they use something like mod_rewrite, you would find something like the following in their config file:

RewriteRule ^(.*)/script\.js$ http://foo.com/gen_script?id=$1 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜