Link to home
Start Free TrialLog in
Avatar of Syswatch6000
Syswatch6000Flag for Denmark

asked on

Javascript onload instead of click

Hello,
I'm trying to implement som jQuery plugin, but I'm not that strong in javascript/jQuery.

Im using the fbmodal script here:
http://www.rrpowered.com/2011/12/fbmodal-jquery-plugin/

The examples is only if you click a link, but I would like the popup box to load as soon as the page is finished loading.
How do I do that ?

This is the link:
<b>Click to open:</b><a href="javascript:void()" id="open" name="open">Modal 1</a><br />

<script type="text/javascript">
$(function(){
$("#close").click( function(){
$(".test").fbmodal({close:true});
});
$("#open").click( function(){
$(".test").fbmodal({
        title: "FaceBook Modal Demo",  
       cancel: "Cancel",
         okay: "Okay",
   okaybutton: true,
 cancelbutton: true,
      buttons: true,
      opacity: 0.0,
	  fadeout: true,
 overlayclose: true,
     modaltop: "30%",
   modalwidth: "400" 
});  
}); 
});
</script>

Open in new window


And if you Press Okay button, it should redirect to another page....?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

so, you want to call the script in the body online?!
http://www.w3schools.com/jsref/event_body_onload.asp

so, instead of
<script type="text/javascript">
$(function(){.... };
</script> 

Open in new window

you put this:
<script type="text/javascript">
$myredirectfunction(){.... };
</script> 

Open in new window

this script must be in the head, and called in the <body onload="myredirectfunction();"> ...
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the live action http://jsbin.com/bofif/1/ with code below
<!DOCTYPE html>
<html>
<head>
  <style>
    #fbmodal{
position: absolute;
left: 0;
z-index: 1000;
text-align: left;
}

#fbmodal .popup{
position: relative;
}

#fbmodal table{
border-collapse: collapse;
}

#fbmodal td{
border-bottom: 0;
padding: 0;
}

#fbmodal .content{
padding:10px;
font-size:11px;
}

#fbmodal .container{
border:solid #555555 1px;
border-top:none;
}

#fbmodal .body{
background: #ffffff;
overflow:hidden;
}

#fbmodal .loading{
text-align: center;
}

#fbmodal .image{
text-align: center;
}

#fbmodal img{
border: 0;
margin: 0;
}

#fbmodal .footer{
background: #F0F0F0;
border-top: 1px solid #DDDDDD;
padding: 8px;
height:26px;
text-align: right;
}

#fbmodal .tl, #fbmodal .tr, #fbmodal .bl, #fbmodal .br{
height: 10px;
width: 10px;
overflow: hidden;
padding: 0;
}

#fbmodal_overlay{
position: fixed;
top: 0px;
left: 0px;
height:100%;
width:100%;
}

.fbmodal_hide{
z-index:-100;
}

.fbmodal_overlay{
background-color: #000;
z-index: 99;
}

#fbmodal .title{
padding:6px 6px 6px 8px;
background:#6c83b3;
border:solid #3b5998 1px;
border-bottom:none;
font-weight:bold;
font-size:14px;
color:#ffffff;
}

#fbmodal .button_outside_border_blue{
border:solid #29447e 1px;
border-bottom:solid #1a356e 1px;
cursor:pointer;
float:left;
}
 
#fbmodal .button_inside_border_blue{
padding:4px 6px 4px 6px;
background-color:#5c75a9;
border-top:solid #8a9cc2 1px;
text-align:center;
font-weight:bold;
font-size:12px;
color:#ffffff;
}

#fbmodal .button_outside_border_grey{
margin-left:5px;
border:solid #9b9b9b 1px;
cursor:pointer;
float:left;
}
 
#fbmodal .button_inside_border_grey{
padding:4px 6px 4px 6px;
background-image:url('fbmodal_cancel.png');
border-top:solid #ffffff 1px;
text-align:center;
font-weight:bold;
font-size:12px;
color:#333333;
}

#fbmodal .right{
float:right;
}

#fbmodal .clear{
clear:both;
}
 
div.button_inside_border_blue:active{
background-color:#4f6aa3;
}


.test{
display:none;
}
    </style>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- for testing only, use your own js -->
<script src="http://jsbin.com/jizim/1.js"></script>  
 <script>
   $(function () {
    $("#close").click(function () {
        $(".test").fbmodal({
            close: true
        });
    });

    $(".test").fbmodal({
        title: "FaceBook Modal Demo",
        cancel: "Cancel",
        okay: "Okay",
        okaybutton: true,
        cancelbutton: true,
        buttons: true,
        opacity: 0.0,
        fadeout: true,
        overlayclose: true,
        modaltop: "30%",
        modalwidth: "400"
    });

});
   </script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="test">This is my modal</div>
</body>
</html>

Open in new window

Avatar of Syswatch6000

ASKER

Scott:
Thanks that just what I needed...

According to the demo solution #10 here:
http://www.rrpowered.com/demo/fbmodal/

it should be possible to do something, based on wheter people press okay/cancel.
If they press okay, it should redirect to another page, or if they press cancel it should just close, doing nothing.

This does NOT work:
$(function(){

$(".alreadymember").fbmodal({
        title: "FaceBook Modal Demo",
        cancel: "Cancel",
        okay: "Okay",
        okaybutton: true,
        cancelbutton: true,
        buttons: true,
        opacity: 0.0,
        fadeout: true,
        overlayclose: true,
        modaltop: "30%",
        modalwidth: "400"}
        ,function(callback){
             if(callback == 1){
                window.location="index.php";
            }
			if(callback == 2){
                $("#display").html("Cancel button clicked.");
            }
        });  
   
	});

Open in new window

The reason is the js code to download and the js code in the sample must be different. I racked my brain trying everything.  Finally I tried switching out the js link from the code I downloaded to the one in the sample and it worked.

Since we have your original question answered, please close this out and if you need help on the callback function, just post another question.

Live action sample http://jsbin.com/bofif/3/edit
<!DOCTYPE html>
<html>
<head>

  <style>
 #fbmodal {
    position: absolute;
    left: 0;
    z-index: 1000;
    text-align: left;
	font-family: "lucida grande", tahoma, verdana, arial, sans-serif;
    font-size: 12px;
    }

#fbmodal .popup{
    position: relative;
    }

#fbmodal table {
    border-collapse: collapse;
    }

#fbmodal td {
    border-bottom: 0;
    padding: 0;
    }

#fbmodal .content {
    padding: 10px;
    font-size: 11px;
    }

#fbmodal .container {
    border: solid #555555 1px;
    border-top: none;
    }

#fbmodal .body {
    background: #ffffff;
    overflow: hidden;
    }

#fbmodal .loading {
    text-align: center;
    }

#fbmodal .image {
    text-align: center;
    }

#fbmodal img {
    border: 0;
    margin: 0;
    }

#fbmodal .footer {
    background: #F0F0F0;
    border-top: 1px solid #DDDDDD;
    padding: 8px;
    height: 26px;
    text-align: right;
    }

#fbmodal .tl, #fbmodal .tr, #fbmodal .bl, #fbmodal .br {
    height: 10px;
    width: 10px;
    overflow: hidden;
    padding: 0;
    }

#fbmodal_overlay {
    position: fixed;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 100%;
    }

.fbmodal_hide {
    z-index: -100;
    }

.fbmodal_overlay {
    background-color: #000;
    z-index: 99;
    }

#fbmodal .title {
    padding: 6px 6px 6px 8px;
    background: #6c83b3;
    border: solid #3b5998 1px;
    border-bottom: none;
    font-weight: bold;
    font-size: 14px;
    color: #ffffff;
    }

#fbmodal .button_outside_border_blue {
    border: solid #29447e 1px;
    border-bottom: solid #1a356e 1px;
    cursor: pointer;
    float: left;
    }
 
#fbmodal .button_inside_border_blue {
    padding: 4px 6px 4px 6px;
    background-color: #5c75a9;
    border-top: solid #8a9cc2 1px;
    text-align: center;
    font-weight: bold;
    font-size: 12px;
    color: #ffffff;
    }

#fbmodal .button_outside_border_grey {
    margin-left: 5px;
    border: solid #9b9b9b 1px;
    cursor: pointer;
    float: left;
    }
 
#fbmodal .button_inside_border_grey {
    padding: 4px 6px 4px 6px;
    background-image: url("images/fbmodal_cancel.png");
    border-top: solid #ffffff 1px;
    text-align: center;
    font-weight: bold;
    font-size: 12px;
    color: #333333;
    }

#fbmodal .right {
    float: right;
    }

#fbmodal .clear {
    clear: both;
    }
 
div.button_inside_border_blue:active {
    background-color: #4f6aa3;
    }

.test {
    display: none;
    }
    </style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- for testing only, use your own js -->
<script src="http://www.rrpowered.com/demo/fbmodal/jquery.fbmodel.js"></script>  
 <script>
$(function(){
/*Modal with no options*/	
    $("#open2").click( function(){
        $(".test").fbmodal();
    }); 	

/*Modal with options*/	
    $("#open").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    }); 

/*Using an id to close the Modal*/
    $("#close").click( function(){
        $(".test").fbmodal({close:true});
    });	
 
/*Using a callback function*/
    $("#open10").click( function(){
        $(".test").fbmodal({
            modalwidth: "400"}
        ,function(callback){
             if(callback == 1){
                $("#display").html("Okay button clicked.");
            }
			if(callback == 2){
                $("#display").html("Cancel button clicked.");
            }
        });  
    }); 

    $("#open3").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.35,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    });
	
    $("#open4").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "10%",
       modalwidth: "400" 
        }); 
    }); 	
	
    $("#open5").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "800" 
        }); 
    }); 
	
    $("#open6").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: false,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    }); 
	
    $("#open7").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: false,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    }); 
	
    $("#open8").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel",
             okay: "Click me!",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    });

    $("#open9").click( function(){
        $(".test").fbmodal({
            title: "FaceBook Modal Demo",  
           cancel: "Cancel me!",
             okay: "Okay",
       okaybutton: true,
     cancelbutton: true,
          buttons: true,
          opacity: 0.0,
	      fadeout: true,
     overlayclose: true,
         modaltop: "30%",
       modalwidth: "400" 
        }); 
    }); 
	
  
  $('#okay').click(function(e){
    e.preventDefault();
      alert("ok");
    
  });
  
});
   </script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="test">
FBModal the first FaceBook replica dialog<br/>
<a href="javascript: vaoid()" id="close">Close this dialog</a> 
</div>
<b>Demo:</b> <a href="javascript:void()" id="open">Modal 1</a><br />
Normal modal.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open2">Modal 2</a><br />
With no width set.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open3">Modal 3</a><br />	
Opacity set 35%.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open4">Modal 4</a><br />	
10% from top of the page.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open5">Modal 5</a><br />	
Width of 800px<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open6">Modal 6</a><br />	
No okay button.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open7">Modal 7</a><br />	
No cancel button.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open8">Modal 8</a><br />	
Custom okay button.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open9">Modal 9</a><br />	
Custon cancel button.<br />
<br />
<b>Demo:</b> <a href="javascript:void()" id="open10">Modal 10</a><br />	
Using the callback function.<br />
<br />
<font style="color: green"><div id="display"></div></font>


		</div>
</body>
</html>

Open in new window

Thanks for helping out... Everything works now. :-)

Superb user, highly recommended !
Thank you for the compliment.  

For future, you get unlimited points to give out and I understand at one time that was not the case.   When asking questions, it is probably best to just leave the slider in the question wizard set to the default.    You never really know how much time your answer takes. Sometimes the easy ones take longer and vice versa.
Scott, thanks for the comment... I was not aware of that. :-)
Next time I dont touch the slider...

Take care.