Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/240160-returning-a-string-from-a-dialog/
Share on other sites

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?

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!

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.