admin管理员组文章数量:1287654
im pretty new to js/jquery and i was trying to set a varable on one page, store it, move page and then use the varable on the new page.
ive made an a tag like this
a id="ex1" href="/example.hmtl">ONE<a>
that on click would take you to the page /example.html and set a varable called 'image' to 1 for this the jquery id use is
$("#ex1").click(function() {
var image = 1;
});
once your on page example.html id like the value of the varable 'image' to be loaded into the the value of startAtSlide
$('.iosSlider').iosSlider({
startAtSlide : //value of var 'image' goes here,
snapToChildren : true
});
is that possable to do, if so am i going about it in the right way ?
im pretty new to js/jquery and i was trying to set a varable on one page, store it, move page and then use the varable on the new page.
ive made an a tag like this
a id="ex1" href="/example.hmtl">ONE<a>
that on click would take you to the page /example.html and set a varable called 'image' to 1 for this the jquery id use is
$("#ex1").click(function() {
var image = 1;
});
once your on page example.html id like the value of the varable 'image' to be loaded into the the value of startAtSlide
$('.iosSlider').iosSlider({
startAtSlide : //value of var 'image' goes here,
snapToChildren : true
});
is that possable to do, if so am i going about it in the right way ?
Share Improve this question edited Sep 25, 2012 at 21:51 Kevin LaBranche 21.1k5 gold badges54 silver badges77 bronze badges asked Aug 10, 2012 at 12:46 samsam 10.1k40 gold badges114 silver badges166 bronze badges 3- I would suggest using location.hash, Google it – A Person Commented Aug 10, 2012 at 12:49
- possible duplicate of Passing javascript variables between pages – Felix Kling Commented Aug 10, 2012 at 12:56
- possible duplicate of Global Variable usage on document.ready reload – Stephan Muller Commented Jun 5, 2015 at 9:39
6 Answers
Reset to default 7I suggest you take a look at localStorage or you try using cookies (for cookies you have a jQuery plugin if you want to use it)
Refreshing a page or navigating to another (related) page kills the current JavaScript environment and builds another one.
Passing variables from one page to another is not as straightforward as you might think.
The suggestion @siidheesh gave you is to put the contents of some variables in the location-hash, that part of the URL after the # sign. But that of course is limited with regards to structure - you can't put objects only scalar values, and with regards to aesthetics when adding multiple variables.
Most current websites in which you've seen some behavior that looks like this are created using AJAX; which means they do not refresh the page or navigate to another page (ever) they always request the content of that other page (via JavaScript) and when they receive it they load the data into the areas of interest.
There is more than two way:
Use cookies to store your variable and then retrieve it in second page
Pass that variable using url parameter
a id="ex1" href="/example.hmtl?startat=1">ONE<a>
. Then use javascript to parse that parameter (using location.href)
to write data to localStorage use:
localStorage.setItem.image = 1;
to read it from storage use
localStorage.image
that's all
Many have suggested using localStorage
, but I must warn that localStorage
is not universally supported by every browser as of yet.
Cookies are a viable solution, however, for your purposes, I feel location.hash
would be the best solution. Your link would look as follows:
<a href="index.html#1">ONE</a>
And to get that variable in javascript:
var slideNumber = location.hash.substr(1);
Here we use substr to skip the #
provided in the location.hash string
You can use a regex on window.location.href to get values of POST variables.
A function to do it :
function getUrlVars()
{
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value)
{
vars[key] = value;
});
return vars;
}
Then you can get values with getUrlVars()["lang"];
(should return "en")
And simply set them in your url example.html?lang=en
本文标签:
版权声明:本文标题:javascript - creating a variable and 'storing it' when moving between pages, jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741286361a2370291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论