1

I have seen this question before but I haven't found a working solution. The question is quite easy. If I call a page like this.

div.load('page?foo=bar');

I want to be able to retrieve foo in some way an use it in a javascript called by page. But I only manage to obtain the paramethers of the parents url.

I know I can declare variables in the parents javascript code but that is not my preffered way.

So I really hope someone has a solution to this problem.

♥ you guys

5
  • "I know I can declare variables in the parents javascript code but that is not my preferred way." It should be. Commented Aug 4, 2013 at 21:10
  • As in I should want that? Does that mean that the way that I want it is imposible? Commented Aug 4, 2013 at 21:12
  • Yes, and it also doesn't make sense. The loaded page is just a string, any script running from it will read the URL of the current page. But the current page already knows that URL, or it wouldn't have been able to request it. Commented Aug 4, 2013 at 21:27
  • No.. thats not what i mean man. I want the new page to know the url. this will be convenient when you have the same page in your window multiple times. you can give them a urlparamether to distinguish them from eachother. Commented Aug 4, 2013 at 21:35
  • I'm telling you it's not possible with javascript on the client side. Pages requested with Ajax don't have their own window to have an URL, they're just strings. Your only chance is capturing the parameters on the server-side and store them as js variables. Commented Aug 4, 2013 at 23:41

2 Answers 2

2

You could use something like this to parse the URI: http://blog.stevenlevithan.com/archives/parseuri

Then you can access the parameters easily from the parent page:

// Set the link that we want to load/examine
var link = 'page?foo=bar';

// Load the link content (as per your code)
div.load(link);

// Grab whatever variables we want from the link
var uri = parseUri(link);
var foo = uri.queryKey.hasOwnProperty('foo') ? uri.queryKey.foo : false;
alert(foo);

EDIT: As bfavaretto already commented, the content loaded in via AJAX is just a string. It's not a page that will be aware of its URI.

However, if you really want the loaded content to be able to access its URI, just make it available in the content itself. For example:

$('#my_div').load('page?foo=bar)

And in the content of "page?foo=bar":

<div class="container" data-page-uri="{{ insert uri here with php, ruby, whatever }}">
    <!-- my page content -->
</div>

Now in your loaded content, you can determine the URI by finding the relevant div with the "data-page-uri" data attribute. Once you have the link, you said that you know how to grab the parameters from it...

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

As I said this is something that I know to do. the question is how I do obtain the uri in the page that is loaded into the div, without acccessing the parents javascript variables.
0

I think you have two solutions. One, if page has a hidden div, with the data needed, the second one, probably the ajax response object has the caller url. You should study the response xhr object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.