开发者

Script prints the name of the variable instead of its value

I'm using this 开发者_如何学编程script that takes an url and modifies it. It stores the new URL in a variable called #url and then it sets that variable as the href of a link.

It's changing the HREF however instead of printing the URL it's creating a link to the name of the variable: #url

This is the script:

    if(url.match(/http:\/\//))
    {
    url = url.substring(7);
    }
    if(url.match(/^www\./))
    {
    url = url.substring(4);
    }
    url = "www.chusmix.com/tests/?ref=" + url;
    $("#output").html(url);
    $("#url").val(url).focus().select();
    var yourElement = document.getElementById('test');
 yourElement.setAttribute('href', '#url');

I'm trying to make it work in JSFiddle, I just tried chaning the quotes but didn't work.

http://jsfiddle.net/Lisandro/JKxRg/4/

Thanks for any help


Remove the single quotes and no need to update the val if you going to change the attribute later.

 yourElement.setAttribute('href', url);


yourElement.setAttribute('href', '#url');

'#url'?

You're printing a literal, so why are you surprised when you see a literal. jhanifen has given you the correct answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜