Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript bookmarklet and URL encoding

Tags:

Fully disclosing that I do not know Javascript, I'm trying to get this Javascript:

javascript:location = 'http://validator.w3.org/check?uri=' +escape(location)&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';

to work as a Bookmarklet in order to send a URL of this format:

http://validator.w3.org/check?uri=http://www.wordpress.org&charset=%28detect+automatically%29&doctype=Inline&ss=1&group=0&user-agent=W3C_Validator%2F1.654

to the W3C valdiator.

I'm URL encoding the Javascript with this encoder, but of course, I'm doing something wrong, either in my Javascript or in the process of encoding it.

Anyone have some ideas in particular or in general about Javascript bookmarklets and URL encoding? Thanks.

like image 592
markratledge Avatar asked Oct 26 '09 16:10

markratledge


People also ask

How do bookmarklets work?

A bookmarklet is a bookmark stored in a web browser that contains JavaScript commands that add new features to the browser. They are stored as the URL of a bookmark in a web browser or as a hyperlink on a web page. Bookmarklets are usually small snippets of JavaScript executed when user clicks on them.


1 Answers

Two Errors:

  1. You need to access the "href" member of the location object:

    window.location.href = http://foo.com

  2. You have invalid JavaScript:

    javascript:location = 'http://validator.w3.org/check?uri=' +escape(location)PLUS SIGN AND QUOTE MISSING HERE&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';

I recommend using this:

javascript:(function(){window.location.href='http://validator.w3.org/check?uri='+escape(window.location.href)+'&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';})()
like image 133
Jake McGraw Avatar answered Nov 08 '22 09:11

Jake McGraw