Explain A Javasript

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
Could someone please inform me on how this script I found works. I found it on a login page that pops-up and once authenticated, closes and refreshes the page. I'm not sure exactly what it says or how to change it to not use ?sec or &sec but rather just normal page names.
Code:
<script language="javascript"> 
	     function CloseAndReturn(sectionID){
	       if (window.opener)
	        {
	        var url=window.opener.location.href;
	        if (url.indexOf("sec=")>-1) 
	        {
	            var firstpart;
	            var middlepart;
	            var lastpart="";
	            firstpart=url.substring(0,url.indexOf("sec="));
	            middlepart=url.substring(url.indexOf("sec="));
	       	if (middlepart.indexOf("&")>-1)
	            lastpart=middlepart.substring(middlepart.indexOf("&"));
	            window.opener.location.href=firstpart + "sec=" + sectionID + lastpart;
	        }  
	        else  
	        {
	            if (url.indexOf("?")==-1)
	                window.opener.location.href=url + "?sec=" + sectionID;
	            else
	                window.opener.location.href=url + "&sec=" + sectionID;
	        }
	        window.close();
	        }
	     }
	   </script>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Code:
<script language="javascript">
     function CloseAndReturn(sectionID){
          if (window.opener) // if the background window is still open, continue
          {
               var url=window.opener.location.href; // url of the background window
               if (url.indexOf("sec=")>-1) // if the background url contains "sec"
               {
	            var firstpart;
	            var middlepart;
	            var lastpart="";
	            firstpart=url.substring(0,url.indexOf("sec=")); // everything before "sec"
	            middlepart=url.substring(url.indexOf("sec=")); // everything including and after "sec"
                    if (middlepart.indexOf("&")>-1) // if there is "&" in middlepart
	            lastpart=middlepart.substring(middlepart.indexOf("&")); // lastpart is everything including and after "&"
	            window.opener.location.href=firstpart + "sec=" + sectionID + lastpart; //sets the new url for the background page
               }  
               else  // "sec" is not in the URL, make a new URL with it
               {
                    if (url.indexOf("?")==-1) // if there is no query string
	                window.opener.location.href=url + "?sec=" + sectionID; // add query string "?sec=..."
	            else // if there is a query string
	                window.opener.location.href=url + "&sec=" + sectionID; // add query string "sec=..."
	        }
	        window.close(); // close this window
	        }
	     }
	   </script>

I hope that helps :)
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
You should really just use a normal form, or AJAX if you want to be fancy. Popup windows distract the user, distract the user, and make them think "why is that there" rather than focusing on the site.
 
Top