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

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

WBOY
WBOYasal
2016-07-06 13:54:181073semak imbas

再诸如【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>


<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>

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn