Pb with a target_blank link with Mootools

余生颓废 提交于 2019-12-25 16:57:09

问题


within a Joomla 1.6 architecture, I want to apply a target_blank to all external links with the use of window.location.hostname.

My (not very pretty) code is thus : http://jsfiddle.net/Y54Me/

But as can be seen, this doesn't apply as I want to certain kind of href, like a href="javascript:;".

I'd be glad to get any advice.

TKS.


回答1:


In this line:

if(link.hostname != window.location.hostname) {
    link.addClass('external');

Change to

    if(link.hostname != window.location.hostname && !link.hostname.search('javascript:')) {
        link.addClass('external');



回答2:


if your links locally do not contain the domain, you can just target them via CSS2:

a[href^="http://"], a[href^="https://"], a[href^='javascript'], a[href^='#'] {
    background: url('http://code.google.com/webtoolkit/tools/gwtdesigner/userinterface/images/globe3.png') 100% 60% no-repeat;
    padding:  0 25px 0 0;
}

and matching that via a selector for $$:

// 1.12
$$("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").addClass("external");

// or for mootools 1.2.5+ 
document.getElements("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").addClass("external").addEvent("click", somefunc);

no looping, regex, string manipulations etc required.

otherwise, you can filter the result of the above via:

var hostname = "jsfiddle.net";

$$("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").filter(function(link) {
    return !link.get("href").contains(hostname);
}).addClass("external");

http://www.jsfiddle.net/dimitar/Y54Me/1/ and http://www.jsfiddle.net/dimitar/Y54Me/2/ for 1.12 (.getProperty instead of .get)

finally. rather than attach click events to them and then window.open, why not just do .set("target", "_blank") instead? that's the most semantic and clean approach imo.



来源:https://stackoverflow.com/questions/4970912/pb-with-a-target-blank-link-with-mootools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!