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
Add a ment  | 

6 Answers 6

Reset to default 7

I 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:

  1. Use cookies to store your variable and then retrieve it in second page

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

本文标签: