Assigning a url subdomain that automatically navigates to a page and calls a javascript function
I am creating a website, and I have javascript function that will play a video playlist by passing it a playlist ID. I want to be able to share a url that points to my website and also calls a javascript function, like:
http://www.yourdomain.com/p/myplaylist
If someone clicks/enters that url, they redirect to my website and the getPlaylist(id) function is called. Is there a way I can do 开发者_运维技巧this?
You would call your javascript on Doucment Load and you can clean up the URLs with RewriteEngine http://en.wikipedia.org/wiki/Rewrite_engine
you can do it
//this function will return value from query string
function QS(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
//this will bind this method to body.onload
$(document).ready(function(){
getPlaylist(QS("id"));
});
I would consider using query strings to determine which playlist should be sent to the client.
For example:
your link on another page:
http://www.yourdomain.com/p/myplaylist?id=1234
and your script
<script>
window.location = '';
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
return "";
}
my playlistId = getQueryVariable('id');
</script>
If you've never used query strings before, you can read up on them just about anywhere. Also, If you are relying on server code to pass the playlist id, the query string is just as available there as is to the client side.
精彩评论