Javascript onclick 页面跳转到首页,没有任何绝对路径

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

onclick page go to homepage without any absolute path

javascriptjqueryhtml

提问by supersaiyan

I want to click on link and I want to go to the page on home page by it self

我想点击链接,我想自己去主页上的页面

<a onclick="javascript:GoToHomePage()" href="javascript:void(0)">Home page</a>

function is

功能是

function GoToHomePage()
{
    window.location = 'default.aspx';   
}

but i want the url of page www.testtest.com instead of www.testtest.com/default.aspx

但我想要页面 www.testtest.com 而不是 www.testtest.com/default.aspx 的网址

I can't even give the absolute path like www.testtest.com as it's a portal and same page coming in few more portals.

我什至不能给出像 www.testtest.com 这样的绝对路径,因为它是一个门户网站,而且同一个页面会出现在更多门户网站中。

回答by Anoop

following code will take you to '//www.testtest.com/'

以下代码将带您到“//www.testtest.com/”

   function GoToHomePage()
  {
    window.location = '/';   
  }

回答by Erwin

Try this:

尝试这个:

window.location = '/';

Executing this code will send your visitor directly to the root of your website.

执行此代码会将您的访问者直接发送到您网站的根目录。

回答by Denys Séguret

It looks like you need

看起来你需要

window.location = '/default.aspx';

or

或者

window.location = '/';

(not sure of your goal)

(不确定你的目标)

The '/' means "the root of the domain".

“/”表示“域的根”。

回答by Muthu Kumaran

Add /before default.aspxand it should take you to home page

添加/之前default.aspx,它应该带你到home page

function GoToHomePage()
{
    window.location = '/default.aspx';   
}

回答by JerryGoyal

I would strongly recommend to use href instead of onclick due to various obvious reasons:

由于各种明显的原因,我强烈建议使用 href 而不是 onclick:

<a href="/">Home page</a>