javascript 如何使用javascript返回主页?

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

How to return to home page using javascript?

javascript

提问by Mud Hut

i am a newbie to jscript. i have used this code in javascript to return to home page

我是 jscript 的新手。我在 javascript 中使用此代码返回主页

function gohome()
{
window.location="../index.html"
}

and i am calling that function in this code

我在这段代码中调用该函数

'<a accesskey="h" id="home" href="javascript:gohome();">Home<\/a>' +

there will be link on a page when it is clicked it will call gohome() function. but same link is appearing on the index page.when clicked it is showing page not found.

单击页面上会出现链接,它将调用 gohome() 函数。但相同的链接出现在索引页上。点击时显示未找到页面。

How to make this link hide in index.html page?

如何让这个链接隐藏在 index.html 页面中?

Can anyone help me??

谁能帮我??

回答by Andrea Ligios

Add .href:

添加.href:

function gohome()
{
window.location.href="../index.html"
}

回答by Ian Overton

I don't think you need a javascript function to do go home you can just do this:

我认为你不需要一个 javascript 函数就可以回家,你可以这样做:

<a accesskey="h" id="home" href="../index.html">Home<\/a>

if you don't want it to show on the home page though you can do something like this assuming the home page is example.com/index.html

如果您不希望它显示在主页上,但假设主页是 example.com/index.html,您可以执行类似的操作

if(window.location.pathname=="/index.html"){
 document.getElementById('home').style.display = 'none';
}

Not in the function, but just called sometime after the link in the source code or in the head.

不是在函数中,而是在源代码或头部中的链接之后的某个时间调用。