swt83
swt83

Reputation: 2011

jQuery - Trigger Popup w/out Click

I have this at above the body tag in my page --

<script type="text/javascript">
    $(document).ready(function() {
        $('#button').popupWindow({windowURL:'http://something',centerBrowser:1,width:400,height:300}); 
    });
</script>   

Is there a way to make this popup happen without the user actually clicking a button -- programmatically w/ code inserted into the middle of the page with php on page load?

Upvotes: 1

Views: 7141

Answers (6)

lipanski
lipanski

Reputation: 1763

I can't comment on the accepted solution, so I'll just post this here - regarding Jonathan Sampsons last proposition (which is really clever): on latest Firefox & Chrome the element, that triggers the click event, must be visible. So you can't really generated a non-rendered element.

Also, if you're using an element from your page, just choose one that doesn't have any other click event defined yet - otherwise all other click events will trigger, which might not be the desired outcome.

Upvotes: 0

JonnyReeves
JonnyReeves

Reputation: 6209

As stated by others, some browsers (notably Microsoft Internet Explorer) will refuse to open a JavaScript popup window unless it was triggered as a result of user interaction. You can easily fake this with a bit of jQuery:

// Create a 'fake' a tag, this will never be added to DOM.
$('<a href="#" />')

    // Register a click handler which will open a new popup window.
    .click(function () { 
        window.open(url, name, options);
    })

    // Simulate the click in jQuery - this will open the popup window.
    .click();

Upvotes: 0

Sampson
Sampson

Reputation: 268344

Add the following next.

$("#button").trigger("click");

Or put it all on on chained line:

$('#button').popupWindow({...options...}).trigger("click");

For what it's worth, you can invoke this from a non-rendered element:

$("<div></div>").popupWindow({..options..}).click();

Upvotes: 2

Rowno
Rowno

Reputation: 3394

I'd suggest using a jQuery UI Dialog. That way you won't have to worry about how different browsers handle popups differently.

Upvotes: 0

Mathew
Mathew

Reputation: 8279

popupWindow is hard-coded to work with links, and only responds to "click" events. That said, if you want the popup to appear on page load, you could try editing line 6 of jquery.popupWindow from this;

$(this).click(function(){

to this;

$(this).ready(function(){

add an id to your body element;

<body id="popuptrigger">

and call the plugin;

$('body#popuptrigger').popupWindow({ ... });

This is untested, but even if it works it's not a very elegant solution. I'd suggest looking around for a popup plugin that allows you more flexibility. I use jqModal to load internal url requests for popup forms and can throughly recommend it. Your mileage mey vary though, so do a google search for "jquery modal" and check out some alternatives.

Upvotes: 0

John Boker
John Boker

Reputation: 83699

I think for security reasons a lot of browsers will not open a popup window without some kind of user action.

Upvotes: 1

Related Questions