JavaScript in window.open () and Window.location.href

Source: Internet
Author: User

1.window.location is a property of the Window object, and Window.Open is a method of the Window object

Window.location is your reference to the URL address object of the current browser window!
Window.Open is a function to open a new window!

2. JavaScript is usually used to link buttons, tables, cells, drop-down lists, and Div. As with regular links, we may need to have the link page open in the current window or open in a new window, so we can use one of the following two items:
window.open to open a new window
Window.location is used to replace the current page, i.e. reposition the current page
You can test it with one of the following examples.
<input type= "button" value= "New window opens" onclick= "window.open (' http://www.google.com ')" >
<input type= "button" value= "current page open" onclick= "window.location= ' http://www.google.com/'" >

3. window.location or window.open how to specify target? 
This is a frequently encountered problem, especially when using frame frames  
Workaround:  
Window.location to Top.location to the specified page at the top  
or  
window.open ("Your url", "_top" );  

4. window.open () is the address of another website that can be opened on one website, while Window.location () is a Web page that opens this site only in one site
window.open () detailed
<script type= "Text/javascript" >
<!--window.open (' page.html '); -
</script>
Because it's a javascripts code, they should be placed between <script language= "JavaScript" > tags and </script>. <!--and is useful for browsers with low versions, where the code in the tags is not displayed as text in these old browsers.
It's a good habit to develop.
window.open (' page.html ') is used to control the popup new window page.html, if page.html is not in the same path as the main window, the path, absolute path (http://) and relative path (. /) are available. You can use both single and double quotes, just don't mix. This section of code can be added anywhere in the HTML,
After setting the popup window

Let's talk about the settings for the popup window. Just add a little something to the code above. Let's customize the appearance of this pop-up window, size, pop-up position to fit the specific situation of the page.

  

<SCRIPTLANGUAGE= "JavaScript">     <!--window.open ('page.html', 'NewWindow', 'height=100, width=400, Top=0, left=0, Toolbar=no, Menubar=no, Scrollbars=no, resizable=no,location=n O, Status=no ') //This sentence should be written in one line     - </SCRIPT> 



Parameter explanation:

window.open command to pop up a new window;
The filename of the ' page.html ' pop-up window;
The name of the ' NewWindow ' pop-up window (not the file name), not required, available empty ' ' instead;
height=100 window height;
width=400 window width;
The pixel value of the Top=0 window from the top of the screen;
The pixel value of the left=0 window from the left side of the screen;
Toolbar=no whether the toolbar is displayed, yes is displayed;
Menubar,scrollbars represents the menu bar and scroll bar.
Resizable=no whether the window size is allowed to change, yes is allowed;
Location=no whether the address bar is displayed, yes is allowed;
Status=no whether the information in the status bar is displayed (usually the file is already open), yes is allowed;
</SCRIPT>


5. Use the function to control the popup window:

The following is a complete code.
  

<HTML>     <Head>         <ScriptLANGUAGE= "JavaScript">             <!--                 functionOpenwin () {window.open ("page.html", "NewWindow", "height=100, width=400, toolbar =no, Menubar=no , Scrollbars=no, Resizable=no, Location=no, Status=no") //Write a line}// -        </Script>     </Head>     <Bodyonload= "Openwin ()">any page content ...</Body> </HTML>                     


A function Openwin () is defined here, and the function content is to open a window. There is no use before calling it. How to invoke it?

    method One:<body onload= "Openwin ()" > Browser to read the page when the pop-up window;
    method Two:<body onunload= "Openwin ()" > Pop-up window when the browser leaves the page;
    method Three: call with a connection: <a href= "#" onclick= "Openwin ()" > Open a Window </a> Note: the "#" used is a virtual connection.
    method Four: call with a button: <input type= "button" onclick= "Openwin ()" value= "open Window" >



6. Pop up two windows at a time
 
Change the source code slightly:

<script language="JavaScript"> <!--function Openwin () {window.open ("page.html","NewWindow","height=100, width=100, Top=0, Left=0,toolbar=no, Menubar=no, Scrollbars=no, Resizable=no, location=n O, status=no< /c10>")//Write a linewindow.open ("page2.html","NewWindow2","height=100, width=100, Top=1, Left=100,toolbar=no, Menubar=no, Scrollbars=no, Resizable=no, Loca Tion=no, status= No")//Write a line}// -</script>

To avoid the pop-up of the 2 window overlay, use top and left to control the pop-up position do not cover each other. Finally, the four methods mentioned above can be called.
Note: The name of the 2 windows (NewWindows and NewWindow2) is not the same, or altogether empty.

  "main window open file 1.htm, while pop-up small window page.html"

Add the following code to the main window <script language= "JavaScript" >
<!--
function Openwin () {
window.open ("page.html", "", "width=200,height=200")
}
-
</script>
Join <body> Area:
<a href= "1.htm" onclick= "Openwin ()" >open</a>.


  "pop-up window timed off control"

Let's take some control of the popup window, and the effect will be better. If we add a little piece of code to the pop-up page (note that the HTML that joins page.html is not on the main page, otherwise ...), will it be cooler to turn it off automatically after 10 seconds?
First, add the following code to the <script language= "JavaScript" >
function Closeit ()
{
SetTimeout ("Self.close ()", 10000)//MS
}
</script>
Then, and then use <body onload= "Closeit ()" > This sentence instead of page.html Zhongyuan Some <BODY> this sentence can be. (This sentence must not forget to write Ah!) The function of this sentence is to invoke the code that closes the window, and then close the window on its own after 10 seconds. )
  "Add a close button to the pop-up window"

<FORM>
<input type= ' BUTTON ' value= ' off ') {
Openwin ()
Document.cookie= "Popped=yes"
}
}
</script>

Then, with <body onload= "Loadpopup ()" > (note not openwin but Loadpop!) Replace the main Page <BODY> this sentence can be. You can try refreshing this page or re-entering the page, and the window will never pop up again.


Note:
1. Window.location.Reload () and window.location.href=window.location.href;
is to refresh the current page.
Self.location.reload (); Also refresh the meaning of this page;

2. Open a new page with window.open ()
But with window.location.href= "" is opened in the original window.
Sometimes the browser will have some security settings window.open must be blocked. For example, avoid pop-up ads windows.

Reproduced in http://guangcai.iteye.com/blog/518345

http://blog.csdn.net/lingling_jy/article/details/8191107

JavaScript in window.open () and Window.location.href

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.