1

i have this script that will open popup when click any body. how to change instead body click it should be button click or link click before the popup appear? Do i need to create button of anything. code much appreciate.

<script type="text/javascript">
var win = window.open('http://google.com', "popup", 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
if (!win){
//    alert("failed for most browsers");
    document.body.onclick = function () {
         window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
    };
}else {
    var thisdoc = document;
    //Is it Chrome?
    if(/chrome/.test(navigator.userAgent.toLowerCase())){
        setTimeout(function() {
            if ((win.innerHeight > 0) == false){
//                alert("failed for chrome");
                  thisdoc.body.onclick = function () {
                      window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
                  };
             }
        }, 100);
    }else{
       win.onload = function() {
            if ((win.innerHeight > 0) == false){
//                alert("failed for others");
                  thisdoc.body.onclick = function () {
                     window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
                  };
            }
       };
    }
}
</script>

JS : FIDDLE : http://jsfiddle.net/ayiem999/678Ru/ ( WITH BUTTON CREATE. PLEASE PLAY)

MY ANSWER FOR THIS QUESTION FOR REFERENCE :

<button id="3" onclick="reply_click(this.id)">B3</button>

<script type="text/javascript">
function reply_click(clicked_id)
{
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
}
</script>
airi
  • 585
  • 5
  • 21
  • Are you asking whether it's possible to handle a button click without a button? – SLaks Jan 30 '14 at 00:59
  • right now when i click any page the popup will appear.. i do not want that. i want it should be a button click or link click not page body click – airi Jan 30 '14 at 01:00
  • put the `.onclick = function(){}` on a button element instead of body. – Patrick Evans Jan 30 '14 at 01:04
  • possible duplicate of [OnClick without jQuery](http://stackoverflow.com/questions/854772/onclick-without-jquery) – CRABOLO Jan 30 '14 at 01:04
  • no .. i just check .. i want this to be a button not body thisdoc.body.onclick = function () <-- how to change to button – airi Jan 30 '14 at 01:06

2 Answers2

1

Assuming you have a button in your page like

<button id="myButton">My Button</button>

then

document.body.onclick = function () {

becomes

$('#myButton').click(function() { ... });
isherwood
  • 58,414
  • 16
  • 114
  • 157
1

If your link or button has an ID of myButton then it's

document.getElementById('myButton').onclick = function () {
     window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
};

This is without jQuery as it isn't needed. If you want to use it instead then it's

$('#myButton').on('click', function(){
    window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
});

Demo

Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58