最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript 'go to page' form script - Stack Overflow

programmeradmin1浏览0评论

I need to setup a form on my site which will enable people to quickly navigate to a page by typing a number into a form and clicking 'go'.

I've got the following code which works in part:

<script type="text/javascript">
function goTo()
{
window.location = document.forms[0].where.value ;
return false;
}
</script>
</head>

<body>
<form action="" method="post" onsubmit="return goTo()">
<input type="text" name="where" value="Enter page number" onfocus="if(this.value ==     this.defaultValue) this.value = '';" onblur="if(this.value == '') this.value = this.defaultValue;">
<input type="submit" value="Go">
</form>
</body>
</html>

I'm injecting this code onto a page with a url, for example, like:

www.examplesite/navigate

The form appears just fine on the /navigate page. Now I'd like my users to be able to type a number into the form which corresponds to a url on my site. For example to get to:

www.examplesite/1234

the user would type '1234' into the form and press 'Go'.

What's happening instead however is the page the script is trying to open is:

www.examplesite/navigate/1234

Does anyone have any idea how I could modify the script to ensure it always opens a page at the root of the site rather than a subdirectory?

Thanks in advance for any help. Appreciate it.

I need to setup a form on my site which will enable people to quickly navigate to a page by typing a number into a form and clicking 'go'.

I've got the following code which works in part:

<script type="text/javascript">
function goTo()
{
window.location = document.forms[0].where.value ;
return false;
}
</script>
</head>

<body>
<form action="" method="post" onsubmit="return goTo()">
<input type="text" name="where" value="Enter page number" onfocus="if(this.value ==     this.defaultValue) this.value = '';" onblur="if(this.value == '') this.value = this.defaultValue;">
<input type="submit" value="Go">
</form>
</body>
</html>

I'm injecting this code onto a page with a url, for example, like:

www.examplesite./navigate

The form appears just fine on the /navigate page. Now I'd like my users to be able to type a number into the form which corresponds to a url on my site. For example to get to:

www.examplesite./1234

the user would type '1234' into the form and press 'Go'.

What's happening instead however is the page the script is trying to open is:

www.examplesite./navigate/1234

Does anyone have any idea how I could modify the script to ensure it always opens a page at the root of the site rather than a subdirectory?

Thanks in advance for any help. Appreciate it.

Share Improve this question asked Jun 18, 2013 at 9:15 user2471356user2471356 11 silver badge1 bronze badge
Add a ment  | 

1 Answer 1

Reset to default 8

Try this:

window.location = "/" + document.forms[0].where.value ;

/ means root. Without it the path is relative to current folder.
In some cases you can use base tag, have a look: https://developer.mozilla/en-US/docs/Web/HTML/Element/base

发布评论

评论列表(0)

  1. 暂无评论