-1

Im developing a Javascript-only app. I have an html page from which I want to load a second html page, passing some parameters to it. In the second page, I want to access these parameters.

Javascript in page1.html:

  var url = "page2.html?foo=bar";
  jQuery("body").load(url);

This loads page2.html, but how can I access the foo parameter in page2.html?

Arne M
  • 87
  • 8
  • Tried this ? http://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js – Vinay Dec 16 '16 at 23:23
  • My question is not how to exctract the foo parameter from the URL string, that's trivial. I want to know how to access the complete URL itself, including foo=bar, from page2.html. I cannot use window.location.href, as that only returns page1.html. – Arne M Dec 16 '16 at 23:35
  • Only server will know what parameters were passed for requesting somethig since js cannot interact with the server i'm afraid this can't be done unless you have some server-side code(php,java,python etc) embedded in page2.html that would tell js inside page2.html about parameters it was requested with. – Vinay Dec 16 '16 at 23:43
  • You have to use the load callback function. jQuery("body").load(url, function(){ //in here you can pull the parameter like you would normally }); (I think) – Chris Dec 16 '16 at 23:44
  • @Novice Yes, it could be easily achieved with php, but I was hoping this would be possible with jQuery's load() (or ajax) method. – Arne M Dec 16 '16 at 23:55
  • @Chris I've tried that. The parameters are not available in the success function of load(). – Arne M Dec 16 '16 at 23:56
  • where is the parameter generated in the first place? – charlietfl Dec 17 '16 at 00:00
  • @charlietfl The parameter is generated in page1.html. I want it to be available to page2.html when page2 is loaded. – Arne M Dec 17 '16 at 00:01
  • You should still have the variables available then because you are just replacing the body you are not actually navigating away or refreshing the page. – Chris Dec 17 '16 at 00:03
  • @Chris I guess you're right, but my actual app is much more complex than this simple example. It would be very much easier if I shomehow could access the parameter being sent. – Arne M Dec 17 '16 at 00:25
  • Why the downvote? – Arne M Dec 17 '16 at 20:35

1 Answers1

0

It seems it's not possible (?) to do get the parameters the way I want. But I found an acceptable workaround: I use localStorage to set and get the parameters instead of sending them to load().

I.e., in page1.html, I do:

localStorage.setItem("foo", "bar");

and in page2.html, I do:

var foo = localStorage.getItem("foo");
MD Ashik
  • 9,117
  • 10
  • 52
  • 59
Arne M
  • 87
  • 8