Solved

Javascript onload instead of click

Posted on 2014-03-03
9
1,060 Views
Last Modified: 2014-03-03
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....?
0
Comment
Question by:Syswatch6000
  • 4
  • 3
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39899810
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();"> ...
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 100 total points
ID: 39899826
You just need to remove the click function.  

$(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"
    });

});

Open in new window

You already have $(function () { and that makes the jquery run after the page loads.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39899839
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

0
 

Author Comment

by:Syswatch6000
ID: 39899918
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

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39900047
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

0
 

Author Closing Comment

by:Syswatch6000
ID: 39900059
Thanks for helping out... Everything works now. :-)

Superb user, highly recommended !
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39900099
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.
0
 

Author Comment

by:Syswatch6000
ID: 39900434
Scott, thanks for the comment... I was not aware of that. :-)
Next time I dont touch the slider...

Take care.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now