像segmentfault.com/faq#q8,“#”后面的内容不仅仅是定位到一个位置,而是还要执行某些操作。怎么实现的呢?

WBOY
Libérer: 2016-07-06 13:54:18
original
1071 Les gens l'ont consulté

再诸如【http://loadpage.b0.upaiyun.com/#http://www.baidu.com】(中括号里的是一个完整的地址)
访问这个页面就会重定向到“#”后面的URL。
“#”后面的内容可以获取吗?是在前端用js获取还是在服务端获取呢?

回复内容:

再诸如【http://loadpage.b0.upaiyun.com/#http://www.baidu.com】(中括号里的是一个完整的地址)
访问这个页面就会重定向到“#”后面的URL。
“#”后面的内容可以获取吗?是在前端用js获取还是在服务端获取呢?

在js里使用 location.hash 就是 # 和它后面的内容.

像segmentfault.com/faq#q8,“#”后面的内容不仅仅是定位到一个位置,而是还要执行某些操作。怎么实现的呢?

如下,假设你的文件名index.html,当你访问index.html#windows.com,会转跳到windows.com

<code>var urlString=window.location.hash.slice(1);//#后面的字符
var pre=urlString.substr(0,7);//[http://]前缀
setTimeout("j()",0)//设置转跳等待时间
function j(){if(urlString===""){}else{if(pre==="http://"){window.location.href=urlString}else{window.location.href="http://"+pre};};}</code>
Copier après la connexion


<code>var qString=window.location.hash.slice(1);//#后面的字符
setTimeout("j()",2000)//设置转跳等待时间
function j(){if(qString==""){}else{
                        if(qString.search(/^http:\/\//)>-1){window.location.href=qString}else{
                            window.location.href="http://"+qString};
                    };
}

</code>
Copier après la connexion

当然你不想获取#后面的字符,而是获取查询内容,即?后面的字符:
window.location.search
,注意两者最好不要混合使用

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!