javascript 中 window.location.href 和 window.onload() 之间的主要区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17829682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Main difference between window.location.href and window.onload() in javascript?
提问by Ajmal
I'm using window.location.href
to load a page and now I have switched over to window.onload()
function, but the page does not load some contents.
我正在使用window.location.href
加载页面,现在我已切换到window.onload()
功能,但该页面未加载某些内容。
window.location.href = $(this).val();
is the code I'm using.
window.location.href = $(this).val();
是我正在使用的代码。
How can I write it using window.onload()
function?
如何使用window.onload()
函数编写它?
回答by Quentin
window.onload
is a property to which you can assign a function that will run when the page has finished loading. It doesn't have a function assigned to it by default, so you can't call it unless you first assign a function to it. It has nothing to do with causing the browser to go to a different URL.
window.onload
是一个属性,您可以为其分配一个函数,该函数将在页面加载完成后运行。默认情况下,它没有分配给它的函数,因此除非先为其分配一个函数,否则无法调用它。它与导致浏览器转到不同的 URL 无关。
Assigning an onload
function directly has also been superseded by addEventListener.
onload
直接分配函数也已被addEventListener取代。
You could assign a function to it that would set location.href
to a new value
您可以为其分配一个函数,该函数将设置location.href
为新值
// Don't do this
function redirect() {
location.href = "http://example.com";
}
addEventListener('load', redirect);
… but if you are doing that as soon as the page loads then you should be using an HTTP redirect instead.
...但如果您在页面加载后立即执行此操作,那么您应该改用 HTTP 重定向。
回答by Arun Bertil
Both are entirely different concepts
两者是完全不同的概念
The window.onload
event is a standard event in the DOM,while window.location.href
returns the location of the current page.
该window.onload
事件是DOM中的标准事件,同时window.location.href
返回当前页面的位置。
回答by HIRA THAKUR
Both of them are used in totally different context.
它们都在完全不同的上下文中使用。
onload is an event
onload 是一个事件
location.href is a property
location.href 是一个属性
*window.onload*
This event is used if you want to take some action the moment page content gets loaded entirely(this includes all your elements,images etc etc).So you can assign any function according to your functionality if your project demands some action after content is loaded.
如果您想在页面内容完全加载时执行某些操作(包括所有元素、图像等),则使用此事件。因此,如果您的项目在内容加载后需要执行某些操作,则可以根据您的功能分配任何功能.
*window.location.href*
This is used for REDIRECTION purpose.I will use it only when I want my user to go to some different page.
这用于重定向目的。我只会在我希望我的用户转到某个不同页面时使用它。
In your ContextIt is pointless to execute window.location.href on window.onload event.If you are not allowing user to spend some time on the onloading page,then I would suggest to directly load the page that you are including in
在您的上下文中在 window.onload 事件上执行 window.location.href 是没有意义的。如果您不允许用户在加载页面上花费一些时间,那么我建议直接加载您包含的页面
window.location.href="this url shoulod be loaded directly instead";
why take a longer route when the shorter one is so efficient??
当较短的路线如此有效时,为什么要选择更长的路线?
回答by Chathura Kulasinghe
window.location.href="url" is what you would call within a javascript function to load some other page(url). It does exactly the same thing what is done when you are clicking a hyperlink on a normal web page.
window.location.href="url" 是您在javascript 函数中调用以加载其他页面(url) 的内容。当您单击普通网页上的超链接时,它执行的操作完全相同。
window.onload() is a an event handler; which you may use to do several things with javascript when an HTML page is loaded. In other words, this will be called, when the page loading event takes place.
window.onload() 是一个事件处理程序;加载 HTML 页面时,您可以使用它来使用 javascript 执行多项操作。换句话说,这将在页面加载事件发生时被调用。
These are two different things.
这是两件不同的事情。
For an example, you can use window.location.href="url" to go to another page, and within that new page you can use window.onload() event handler to execute something you want to happen when that page is loaded.
例如,您可以使用 window.location.href="url" 转到另一个页面,在该新页面中,您可以使用 window.onload() 事件处理程序来执行您希望在加载该页面时发生的事情。
eg:
例如:
<script>
window.onload=function(){
document.write("hi! this text was written when the page was being loaded");
}
</script>
Thanks.
谢谢。