ginerjm Posted June 22, 2011 Share Posted June 22, 2011 ok - I've been using a modaldialogbox to handle a login process. It all works fine, but I'm trying to add a returned string value and want to use it in the calling script's function that triggered the dialog. This is my calling code snippet which is in a js function myWindow = window.showModalDialog(....); alert("back in caller with a value "+myWindow); This is my code from the called dialog box's return function: function CloseMe(uid) { alert("about to pass back "+uid); self.close(); return ('uid'); } The alert in the return shows a valid value for "uid", so I'm expecting it to be returned to the caller and be in "myWindow", but the alert in calling code shows the returned value as undefined. Took me a while to get this much of it working - I even have the follow-up code to handle the returned value ready to go, so what am I missing at this point? I tried putting my return var in the "close()" line but that didn't work either. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 23, 2011 Share Posted June 23, 2011 The way you get a return value wiht showModalDialog is to set the window.returnValue property of the document you load as the first parameter. Also: return('uid'); Would return a string 'uid' not the local uid variable in CloseMe, but perhaps you knew that? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2011 Author Share Posted June 23, 2011 actually that piece of the snippet was out-dated. Just an experiment from some sample code I found. Which didn't work either. As for your suggestion can you give me a sample? Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 23, 2011 Share Posted June 23, 2011 I assume you have some javascript in the document you are loading. I'm going to also assume it's a simple form. So you might have some onsubmit code that takes an input on the form. Let's assume that input is named 'username'. In the onsubmit you'd have: window.returnValue = window.document.forms[0].username.value; Before wasting too much time on this, I hope you realize that showModalDialog is an IE only function! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2011 Author Share Posted June 23, 2011 No - did not know that. Is there a more general purpose method or doesn't anyone else support modal windows? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2011 Author Share Posted June 23, 2011 ok - I found an example of using returnValue. Added the code and here's what happens. my dialog alert shows that I have a valid value to return to the caller. my caller js routine shows that the value received is correct Yet - my use of the following statement: var myWindow = window.showModalDialog(url,"","dialogWidth:400px;dialogHeight:350px;center:yes;status:no;resizable:no"); document.getElementById('uiddiv').innerHTML = "New User is "+myWindow; yields the following: New User is false What am I doing wrong now? for those who need refreshing here is my code: caller has var myWindow = window.showModalDialog(url,"","dialogWidth:400px;dialogHeight:350px;center:yes;status:no;resizable:no"); alert("back in openlogin with "+myWindow); if (myWindow='' || !myWindow) { document.getElementById('uiddiv').innerHTML = ""; } else { document.getElementById('uiddiv').innerHTML = "New User is "+myWindow; } And my called window's code is function CloseMe(uid) { window.returnValue=uid; alert("in closeme with "+uid); self.close(); return; } All of my alerts show what I expect them to. It's just the very last statement in my caller that yields the unexpected. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 23, 2011 Share Posted June 23, 2011 This guy talks about the basic technique most people use: http://raventools.com/blog/create-a-modal-dialog-using-css-and-javascript/ And this article has all sorts of implementations including ones based on popular js frameworks like prototype and jquery: http://www.designlabelblog.com/2009/03/20-ways-to-create-javascript-modal.html Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2011 Author Share Posted June 23, 2011 Thanks for the references. Looks very doable! Altho I do wonder what I'm doing wrong here. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 23, 2011 Share Posted June 23, 2011 I would have to see all the code. You keep posting snippets but omitting key things, so I'm left wondering if there's a critical piece that is not in the right place. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2011 Author Share Posted June 23, 2011 Here is the entire calling function that is not getting the returned value: function OpenLoginWin() { var url = 'myurl'; var myWindow = window.showModalDialog(url,"","dialogWidth:400px;dialogHeight:350px;center:yes;status:no;resizable:no"); alert("back in openlogin with "+myWindow); if (myWindow='' || !myWindow) { document.getElementById('uiddiv').innerHTML = ""; } else { document.getElementById('uiddiv').innerHTML = "New User is "+myWindow; } } Here is the function that is returning to the above code: function CloseMe(uid) { window.returnValue=uid; alert("in closeme with "+uid); self.close(); return; } As you can see I didn't leave much out. The alert in 'closeme' is showing the correct value to be returned. The alert in the calling function is showing a value of 'undefined'after the return occurs. I think perhaps that I am not utilizing the 'returnValue' statement correctly, but that was how two examples I found were using it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.