网址片段和BASE标签 [英] URL fragments and the BASE tag

查看:119
本文介绍了网址片段和BASE标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用< base> 标签来简化开发。

m知道当锚点只是一个URL片段时发生的 feature ,因为它会路由到< base> URL +片段。



我能做些什么来规避这种情况?我从来没有冒充过 window.location 或Javascript中的任何东西,而不是绕过它一段时间,我假设有人知道一个快速和肮脏,或一个例子。



这可以绕过吗?如果是这样,请指教。



我讨厌提出一些问题,表明没有尝试过,但我一直在寻找/ em>)



另外;使用jQuery,所以任何可以利用vanilla Javascript或jQuery的例子都是可以的)






jQuery解决方案我一起入侵,不确定它是否可行作为永久解决方案, ($(this).attr('))
$ b $ $ $ $ $ $ $ $''' ('#')== 0){
$(this).attr('href',(window.location.href).replace(/#.*$/,'')
+ $(this).attr('href'));
}
});


解决方案

好吧,我想我现在明白了这个问题。



这已经在Google Chrome和Firefox中测试过。



在主体的末尾添加此javascript :

  base = document.getElementsByTagName(base); 
base = base [0] .href;

links = document.getElementsByTagName('a');

(链接索引){
if(links [index] .href.indexOf(base +#)== 0){
links [index] .href = document.location +链接[指数] .href.slice(链接[指数] .href.indexOf( #));
}
}


I'm using the <base> tag in an application to simplify development.

I'm aware of the "feature" that occurs when an anchor is only a URL fragment, as in it routes to the <base> URL + fragment.

What can I do to circumvent that? I've never fudged with window.location or anything in Javascript, and rather than hack around for awhile at it, I assume someone knows of a quick-and-dirty, or an example.

Can this be circumvented? If so, please advise.

(I hate asking questions that suggest no attempt has been made, but I've been searching/reading to little avail)

(Also; using jQuery, so any examples that would take advantage of vanilla Javascript or jQuery are welcome)


jQuery solution I hacked together, not sure if its viable as a permanent solution though, thoughts?

$('a').each(function(index){
    if($(this).attr('href').indexOf('#') == 0){
        $(this).attr('href', (window.location.href).replace(/#.*$/, '')
            + $(this).attr('href'));
    }
});

解决方案

Ok, so, I think I understand the question now.

This has been tested in Google Chrome and Firefox.

Add this javascript at the end of the body:

base=document.getElementsByTagName("base"); 
base=base[0].href;

links=document.getElementsByTagName('a');

for(index in links){
    if(links[index].href.indexOf(base+"#")==0){
        links[index].href=document.location+links[index].href.slice(links[index].href.indexOf("#"));
    }
}

这篇关于网址片段和BASE标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆