简体   繁体   English

页面加载时阻止浏览器跳转到#name

[英]Stop browser from jumping to #name when page loads

I have a form in a jQuery dialog box named "login". 我在jQuery对话框中有一个名为“登录”的表单。 In JavaScript, I detect if "#login" is in the URL -- if so, it displays the login dialog box. 在JavaScript中,我检测URL中是否包含“ #login”-如果是,它将显示登录对话框。 In FF/Safari/Chrome it works fine, but in IE6/7 the browser page jumps down to the top of the form when the dialog box is displayed. 在FF / Safari / Chrome中,它可以正常工作,但在IE6 / 7中,显示对话框时,浏览器页面会跳到表单顶部。 I would like to prevent this from happening. 我想防止这种情况的发生。

I found what looked like an answer to my question here , which gives this example code: 我在这里找到了我的问题的答案,它给出了以下示例代码:

$('a.anchor').remove().prependTo('body');

I changed "a.anchor" to "form.anchor", but it doesn't work correctly. 我将“ a.anchor”更改为“ form.anchor”,但无法正常工作。 When I first load mydomain/#login IE still jumps down to the top of the form. 首次加载mydomain /#login时,IE仍跳至表单顶部。 Then if I refresh the page it again displays the dialog box but doesn't jump down. 然后,如果我刷新页面,它将再次显示对话框,但不会跳下来。

Any idea how to globally just prevent IE from jumping to any form/anchor names? 任何想法如何全局只是防止IE跳到任何形式/锚名称?

I'd recommend using a query parameter or something, like page.html?showlogin=true. 我建议使用查询参数或类似的参数,例如page.html?showlogin = true。 The page.html#xxxxx on a URL is meant to scroll the browser to that ID on the page. URL上的page.html#xxxxx旨在将浏览器滚动到页面上的该ID。

http://www.w3schools.com/html/html_links.asp halfway down explains the name attribute. 中途http://www.w3schools.com/html/html_links.asp解释了名称属性。

Otherwise you'll be doing some messy hacks to get around browsers doing what they are supposed to do. 否则,您将进行一些混乱的黑客操作,以绕过浏览器执行他们应做的事情。

........不要在代码中使用不想使用的命名锚点吗?

There are legitimate reasons to use the hash mark this way. 有合理的理由使用这种方式使用井号。 In Ajaxian applications it can help manage the browser history. 在Ajaxian应用程序中,它可以帮助管理浏览器历史记录。

All browsers, including IE, should be ignoring the location.hash unless there is a named anchor -- except that IE likes to jump down to any element with that ID or NAME. 除非有命名锚点,否则所有浏览器(包括IE)都应忽略location.hash,除非IE喜欢跳到具有该ID或NAME的任何元素。 So if you have 所以如果你有

<form name="login" ...

or 要么

<form id="login" ...

or even 甚至

<div id="login"><form name="somethingElse"...

that might cause IE to treat it like the traditional hash mark and jump down. 可能会导致IE将其视为传统的哈希标记并跳下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM