Apply some events and actions on <a> tag after window load()
I have some links in my page and when any link has clicked it goes to another page and make window load.
Bu开发者_Python百科t I want to make some action on the link that has been clicked last and I applied. But when window load happen everything gone.
These links are common to each page.
Please help me. Sorry for any mistakes.
THANKS.
I'm assuming you're trying to do something like highlight the current page's link in a navigation menu. Check this out...
http://docs.jquery.com/Tutorials:Auto-Selecting_Navigation
You will either need to send the variables to the next page via GET or POST
or
Save them in a cookie, a database or in a file on the server.
Let's say the variables are show=1
and page=phones
.
You could send the variables like this:
var show = 1;
var page = 'phones';
$('#link').click(function(){
top.location.href = 'page.html?show='+show+'&=page'+phones+'';
});
Then you could fetch the variables on the next page with either a serverside language like PHP or ASP, or you can create a smart function in javascript to fetch the part of the URL you need:
function getParam(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
and use it like this (in page.html):
var show = getParam('show');
var page = getParam('page');
If you would like to save the variables to a cookie in the users browser you could use these functions:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
And use them like this:
In your first page:
var show = 1;
var page = 'phones';
setCookie(''+show+'',show,5); // expire after 5 days
setCookie(''+page+'',page,5); // expire after 5 days
// now redirect to other page like in the first example
$('#link').click(function(){
top.location.href = 'page.html';
});
In your second page:
var show = getCookie('show');
var page = getCookie('page');
if (show === 1) {
// Do whatever you like, because cookie 'show' is 1
}
I hope it helps!
精彩评论