tags:

views:

27

answers:

3

I have a main page with a js variable. I want to open a modal on that main page and load html content from a different page. I need to pass that variable to the modal that is opening and use it inside the loaded html content. I have seen several questions similar to that in these forums but i don't see any that explain how I can tease that variable out of the loaded html content. I know that i can make a GET request for that external html content, and I know that i can include the variable in the query string. But how do i get it out of the query string inside the modal? All of the examples i see for getting things out of query string involve using window.location.href or something similar. Well that won't work for me because that returns the url of the main page I am on and not my modal popup. Any suggestions?

A: 

VBA - I'd normally use the $("#mypopupdiv").dialog() function and then populate #mypopupdiv with values derived via a $ajax call. you'd store your $var at module level and then when the popup was loading (or loaded, depending on your processing requirements), you'd put the variable to use. The beauty of the .dialog() method is that altho it's a popup, it's actually part of the parent page, rather than being a seperate process.

with careful consideration, this is very reliable and effective and can be used in a wide variety of scenarios without side-effect.

further details if required.

jim

jim
I am using the dialog function. I have a div on my main page that is empty and ready to house the html i pull in. I then wrote a small jQuery plugin that works on the div and loads the html content into it. I think my problem is that the popup is a div in my parent page. That's why window.location always returns the url of the main page which is not where my query string is.my ? boils down to how can you pass a parameter to a modal dialog and then use that param in the dialog?this can be accomplished by using a global js var in the main page but that is what i am trying to avoid.
VBAHole22
VBA - ok, got a better understanding now of the issue. it may help to describe the purpose of the 'variable' that needs to be processed as there may be a tangential way to appraoch the problem that's not obvious from the current info :) for example, i'll often pass back a json result which has status and data 'elements' thus allowing for some kind of post processing based on the status value for example.
jim
it is a user id. So the main form represents a client app and there could be many diff kinds of client app but they all use this same 'shared' html content that is loaded into the div. that content does some editing of the database and i need that user id to audit all of the edits. the client app is responsible for gathering the user id. If i have total control over the client app i get user id from asp.net windows login and then set it to a global js that the shared content uses -no problems. but i don't want that dependency on a global js var.
VBAHole22
as i said above, a well structured json object could be one route. another way (tho it is still like a js var) would be to add the id to the div .data() object. i'll show this in a seperate message...
jim
I like the idea of the data property. That is definitely one way to go. How would i retrieve that in the modal? I realize there has to be some form of a contract between the client and the modal content. I'm just trying to avoid that global js var.
VBAHole22
vba - see below. basically, the contract would be $("#mypopupdiv").data("userid", $varUserId); on the parent side and var userId = $("#mypopupdiv").data("userid"); on the popup side.
jim
A: 

VBA, to keep the popup div 'self-contained' and not using the global var, you could do the following:

$("#mypopupdiv").data("userid", $varUserId);

then, in the popup populate code, you'd 'grab' that data:

if ($("#mypopupdiv").data("userid") != null) {
    // do some stuff with it
}

other ways to skin the cat abound, but just another one to throw to the wind...

jim

jim
i appreciate the help jim. I think i almost have it. When i go to get the value out of the data property of the dialog I am actually already inside the modal running in the document.ready method. So I can't really ref the name of the modal itself, also I wouldn't expect the injected html content to know the name of the div it is in. Right now I can't really figure out what the $this object really refers to when I'm in the doc.ready method on the shared content.
VBAHole22
gotcha - i'll have a think about that... gorra head off now but will respond l8r... (tho of course, you should be able to grab the .data() of the containing parent div (which will be your popupdiv) perhaps as a short-term test for it)
jim
A: 

I got it to work but I'm not sure how or why. In my main page I utilize my jQuery plugin which is named jquery.contacts.js. I set a few default values on that plugin then i give it a div and call the contacts method.

 $.fn.contacts.defaults.currentUser = _currentUser;

        if (!$("#searchContactDialog").contacts()) {
            return false;
        }

This pulls in the external content and loads it into the div. Now when I run the js that is inside that injected content I can ref a variable named contacts and get at those defaults.

_userId = $(this).contacts.defaults.currentUser;

I was not expecting that contacts var to be available to me and I'm not sure how it is so. In fact I'm a little confused as to what $(this) represents in that line of code. But it works...

VBAHole22