window.location(.href)=”URL”
The.href can be omitted
Window.location and window.location.href implement the same effect
Such as:
window.location = "http://www.baidu.com"
window.location.href = "http://www.baidu.com"
Copy the code
Either of these methods can jump from the current page to the target page. The difference is that window.location returns an object, and if it doesn’t have dot href, its default argument is href
winodw.location.assign(“URL”)
This method is similar to the above method:
winodw.location.assign("http://www.baidu.com")
Copy the code
window.location.replace(“URL”)
If you use window.location.replace(“http://www.baidu.com”) to jump to a page, it differs from the above method in that it does not save information about the page after jumping.
So if you use the history back button, it doesn’t work
The effect is similar to right-clicking on a web page and opening in a new window or a new TAB. The window.location.href achieves this by clicking on the target link on the page and then clicking on the history back button to return to the previous page.
History page jump
Either of the following methods can return to the history page by clicking the back button in the upper left corner of the page
window.history.back();
window.history.go(-1);
Copy the code
You can put numbers in both back and Go
For example, putting -1 in it means going back to the previous level, putting -2 in it means going back to the previous level, and so on, and it can also put the specified routing path, such as window.history.go(‘.. /routes/admin/’); This jumps to the specified routing module
meta refresh
If javascript is disabled in the user’s browser, you can use meta Refresh to automatically jump:
<noscript>
<meta http-equiv="refresh" content="0; URL=http://www.baidu.com/">
</noscript>
Copy the code
If you want to implement automatic JS jump, you can add a timer:
setTimeout(function(){ ... }, 1000);
Copy the code
Reload this page
If you run out of code logic and want to reload the page, there are two ways to do it:
window.location.reload()
window.location.replace()
Copy the code
Either method reloads the page, but replace() can lead to a different URL: window.location.replace(“http://www.baidu.com”)
window.navigate(‘URL’)
The window. Navigate (‘URL’) method is for Internet Explorer only, not for other browsers such as Firefox, and is not listed at all in the HTML DOM Window Object, so use it as little as possible and forget it.
Self, parent, this, top
Top.location. href= "url" opens the url in the top page (out of the frame) self.location.href= "url" opens the URL in the parent window This.location. href= "url" is used in the same way as selfCopy the code
If (top.location == self.location) disables frame references by checking whether the current location is the top layer of the page. If (top.location == self.location) The effect is to open the URL in the custom frame window.
Example:
if(top ! = self){ top.location.href = location.href; } // Disable frame referencesCopy the code
The following is an example found on the Internet, not very intuitive, you can add the above three lines of code, you can first remove, add, see the effect, should be clear
Top. HTML code
<script language=javascript>
function rs() {if(top ! == self){ top.location.href = location.href; } parent.left.location.href="top.htm" ;
parent.bot.location.href="top.htm";
}
< /script>
< input type=button name=name value="ksdj"onclick=rs(); >Copy the code
Test. The HTML code
<FRAMESET COLS="150,*">
< FRAME SRC="left.htm" name=left>
< FRAMESET ROWS="150,*">
< FRAME SRC="top.htm" name=top>
< FRAME SRC="bot.htm" name=bot>
< /FRAMESET>
< /FRAMESET>
Copy the code
Give it a try, it might look like this!
Top represents the main window, location represents the current window, and if your file has only one frame, no iframe or frame, then it’s exactly the same, no difference.
Location opens a new page in the top frame window.location opens a new page in the current frame parent. Location opens the Url in the parent window of the current window
JS Common ways to jump and refresh a page
Page jump
window.location.href="index.php"; window.history.back(-1); // Similar to the button, if the argument is negative several, it will back up several times. window.navigate("index.jsp"); The navigate object contains information about the browser and can also be used as a page jump, followed by the place to jump. self.location.href=index.htm; //self refers to the current window object, which belongs to the topmost object of the window; //location.href = 'window'; //self.location.href = 'window'; // The top property returns the parent window at the top level. // This property returns a read-only reference to a top-level window. // If the window itself is a top-level window, the top property stores references to the window itself. // If the window is a frame, the top property refers to the top-level window that contains the frame. location.replace(document.referrer); document.referrer history.go(-1); // do not refresh the page history.back(); // Do not refresh the pageCopy the code
Javascript page refresh
1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)
7 location.replace(location)
8 document.URL=location.href
Copy the code
Automatically refresh
Two fingers are redirected to http://www.baidu.com after 2 seconds
JS automatic page refresh
<script language="JavaScript">
setTimeout('window.location.reload()'// How to refresh the page containing the frame with [javascript] view plain copyprint? <script language=JavaScript> parent.location.reload(); </script> // Child window refreshes parent window [javascript] View plain copyprint? <script language=JavaScript> self.opener.location.reload(); </script> (or <a href="javascript:opener.location.reload()"> Refresh </a>) // How to refresh another frame's page with [javascript] View plain copyprint? <script language=JavaScript> parent. Another FrameID. Location. Reload (); </script> If you want to refresh when the window is closed or when the window is open, call the following statement in <body>.Copy the code
Jquery method
$(location).attr('href'.'http://www.jb51.net');
$(window).attr('location'.'http://www.jb51.net');
$(location).prop('href'.'http://www.jb51.net')
Copy the code