1

I'm trying to create a popup window with quite a bit of content, so I've put the content into a separate .php file and I use the following javascript to get the effect I want:

$('#popup').show();
var u = $("#username").html();
$('#popup').html('<iframe src="content.php?u='+u+'"></iframe>');

This works well, except that I have a close function for when the user hits the escape key:

$(document).keyup(function(e) {
  if (e.keyCode == 27) { 
    $('#popup').hide().html('');
    } 
});

My problem is that once the popup opens, and the user clicks within the iframe, the escape button no longer closes the window because I'm using the iframe.

Is there a better way in Javascript to include an external php file dynamically like this, or is there a way I can make the escape button function work even if the iframe has been clicked within?

2
  • 1
    From the same domain? use Ajax Commented May 11, 2014 at 20:11
  • I didn't know I could use ajax that way. Thanks guys. Commented May 11, 2014 at 20:23

2 Answers 2

2

You can use jQuery's .load():

$('#popup').load('somefile.php');
Sign up to request clarification or add additional context in comments.

Comments

1

Its not advisable to use iframe, use ajax. If you still want to use iframe then you have to store the popup in top, e.g. add below code in main php file

top.popup = $('#popup');
top.popup.show();
var u = $("#username").html();
top.popup.html('<iframe src="content.php?u='+u+'"></iframe>');

And below code in main php and content.php

$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        top.popup.hide().html('');
    }
});

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.