开发者

Copy a javascript URL 'as-is' from a link in a web page?:

I want to copy a javascript URL-char for char. How, for example, would I successfully copy the javascript from the 'View Source' link on this page:

http://javascript.about.com/library/blsource.htm

doing something like(?):

(function(){
    var w=open('','');
    with(w.document) { 
        write(encodeBlahComponent(document.activeElement.href).replace(/blah/g,'asii equivalent').replace(/blah/g,'unicode equivalent').replace(/blah/g,'entity equivalent'));
        close();
    }
})()

What encoding should I use and how to script 开发者_开发百科it properly?


If you're document.write​ing to an HTML document, any text you output would have to be HTML-escaped:

function encodeHTML(s) { // for text content and attribute values with " delimiter
    return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;');
}

somedocument.write(encodeHTML(link.href));

However it would probably be easier to use DOM methods:

somedocument.write('<p id="out">x</p>');
somedocument.getElementById('out').firstChild.data= link.href;

Either way you don't have to worry about Unicode or &#...; character references. JavaScript strings are natively Unicode. And you would only need to think about using encodeURI if you are creating a URI from some script (eg. var uri= 'javascript:'+encodeURI(somejscode)), which you're not here, you've already got a URI in the link. (encodeURIComponent would also work, but for this case where you have a whole URI not just a component, encodeURI will give simpler results.)

PS. You don't want to use the with statement ever, if you can help it. (Or javascript: URLs, for that matter!)

ETA. If you really need the original source with all errors intact, you would have to do like web-sniffer does and fetch the page again from the network. You might do this for the current page as long as it is the result of a GET method, using an XMLHttpRequest. For example:

var d= window.open().document, x= new XMLHttpRequest();
d.write('<body><pre>x</pre>');
d.close();
x.onreadystatechange= function() {
    if (this.readyState===4)
        d.body.firstChild.firstChild.data= this.responseText;
}
x.open('GET', location.href);
x.send(null);

Or, packed into a bookmarklet:

javascript:(function()%7Bvar%20d=window.open().document,x=new%20XMLHttpRequest();d.write('%3Cbody%3E%3Cpre%3Ex%3C/pre%3E');d.close();x.onreadystatechange=function()%7Bif(this.readyState===4)d.body.firstChild.firstChild.data=this.responseText%7D;x.open('GET',location.href);x.send(null)%7D)()


Re: "Encoding is a function that is needed when you go from one context to another, such as from raw text to HTML source. If you take the href of a link, you've got a URL in a string. If the context you want to use that URL in is just as a URL then there is nothing more you need to do. In that case the way to “clone” a JavaScript string is to say just var newstring= oldstring. If the target context is HTML source written by document.write, you only need to HTML-encode it; nothing else.".

Looks like I've been making a mountain out of a molehill. Your advice didn't sink in until now. It was that crud "View Source" bookmarklet that threw me (I thought the author was supposed to be a JS guru!). Another one of those "learning experience" moments I suppose. Never mind. Many thanks for your support. Thread closed and thank you again.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜