JavaScript 在按钮单击时加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3682805/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:42:12  来源:igfitidea点击:

JavaScript load a page on button click

javascriptbuttonload

提问by Patrick

I am trying to do a very simple task here, I would like to be able to click a button on a page and have it take me to another page. I have tried window.location.href, and a bunch of other things and it does nothing. I have tried different platforms and different browsers, all with the same result.

我想在这里做一个非常简单的任务,我希望能够点击页面上的一个按钮,让它带我到另一个页面。我试过 window.location.href 和一堆其他东西,但它什么也没做。我尝试了不同的平台和不同的浏览器,结果都一样。

I know that it can call a function but I just cannot get the new page to load. Also this is all in my local file system and both pages live at the same level (but I have also tried to load an external page like www.apple.com).

我知道它可以调用一个函数,但我就是无法加载新页面。此外,这一切都在我的本地文件系统中,并且两个页面都处于同一级别(但我也尝试加载像 www.apple.com 这样的外部页面)。

Any thoughts?

有什么想法吗?

Thanks Patrick

谢谢帕特里克

回答by Pranay Rana

Simple code to redirect page

重定向页面的简单代码

<!-- html button designing and calling the event in javascript -->
<input id="btntest" type="button" value="Check" 
       onclick="window.location.href = 'http://www.google.com'" />

回答by BGerrissen

Don't abuse form elements where <a> elements will suffice.

不要滥用 <a> 元素就足够的表单元素。

<style>
    /* or put this in your stylesheet */

    .button {
        display: inline-block;
        padding: 3px 5px;
        border: 1px solid #000;
        background: #eee;
    }

</style>

<!-- instead of abusing a button or input element -->
<a href="url" class="button">text</a>

回答by tdammers

Just window.location = "http://wherever.you.wanna.go.com/", or, for local links, window.location = "my_relative_link.html".

只是window.location = "http://wherever.you.wanna.go.com/",或者,对于本地链接,window.location = "my_relative_link.html".

You can try it by typing it into your address bar as well, e.g. javascript: window.location = "http://www.google.com/".

您也可以通过在地址栏中输入它来尝试它,例如javascript: window.location = "http://www.google.com/"

Also note that the protocol part of the URL (http://) is not optional for absolute links; omitting it will make javascript assume a relative link.

另请注意,URL ( http://)的协议部分对于绝对链接不是可选的;省略它会使 javascript 假定一个相对链接。

回答by Tarik

The answers here work to open the page in the same browser window/tab.

此处的答案可在同一浏览器窗口/选项卡中打开页面。

However, I wanted the page to open in a new window/tab when they click a button. (tab/window decision depends on the user's browser setting)

但是,当他们单击按钮时,我希望页面在新窗口/选项卡中打开。(选项卡/窗口决定取决于用户的浏览器设置)

So here is how it worked to open the page in new tab/window:

所以这是在新选项卡/窗口中打开页面的工作方式:

<button type="button" onclick="window.open('http://www.example.com/', '_blank');">View Example Page</button>

It doesn't have to be a button, you can use anywhere. Notice the _blankthat is used to open in new tab/window.

它不必是一个按钮,你可以在任何地方使用。请注意用于在新选项卡/窗口中打开的_blank