Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have Created One script that shows password on hover just like windows 8 on eye click. i have made this functionality through javascript with following code.

function text(){this.type="text"}function password(){this.type="password"}function addHandlers(){var e=false;var t=document.evaluate("//input[@type='password']",document,null,6,null);for(var n=t.snapshotLength-1,r;r=t.snapshotItem(n);n--){if(!e){r.addEventListener("mouseover",text,false);r.addEventListener("mouseout",password,false)}else{r.addEventListener("focus",text,false);r.addEventListener("blur",password,false)}}}addHandlers()

sorry for the packed js. but what i want is create a bookmark or bookmarklet that run this script ..

i tried
javascript:(mycode);

but it didn't work..

share|improve this question
    
bookmark is not very specific. There is more than one program that knows something like a bookmark/bookmarklet. Of course it seems that you mean a browser. Well there is Firefox, IE, Opera etc.. which one? –  humanityANDpeace Jan 8 '13 at 12:33
    
firefox 17..... –  vishal Jan 8 '13 at 12:33

1 Answer 1

up vote 0 down vote accepted

To solve your specific problem I managed to get your Javascript code do execute without any error in this version

javascript:(function(){function text(){this.type="text"};function password(){this.type="password"};function addHandlers(){var e=false;var t=document.evaluate("//input[@type='password']",document,null,6,null);for(var n=t.snapshotLength-1,r;r=t.snapshotItem(n);n--){if(!e){r.addEventListener("mouseover",text,false);r.addEventListener("mouseout",password,false)}else{r.addEventListener("focus",text,false);r.addEventListener("blur",password,false)}}};addHandlers();alert("bookmarklet loaded");})();

To see if your code works I added a alert("bookmarklet loaded"); in the end of the bookmarklet. So you can try and should - since I tested on FF17- see as a result a messagebox appear. If your code works or does not work I am unable to say, but of course this was not the question.

There are some things you can keep in mind ot make transformation of Javascript code within <script></script> tags easier to be adopted for a bookmarklet

  • In normal javascript code a newline can seperate two commands. Bookmarklets are in one line so substitute the linebreak with a semicolon ;.

edit (let's put in an example here)

Lets have an example. Lets have two a sourcecode with two instructions:

var anumber=1
var atext="hallo"

When put all in one line (= make the linebreak to whitespaces) you would first end up with this

var anumber=1 var atext="hallo"

This is not valid Javascript code anymore. If you want to combine two instructions in one line you need to seperate/delimiter the two instructions by using a semicolon like this:

var anumber=1; var atext="hallo"
  • wrap all you code in kind of this kind of closure:
    javascript:(function(){ [...here your code...] })();

There are quite some websites out there to investigate the solution. Like here.

If the solution worked for you, please consider acception this answer as a solution ;)

share|improve this answer
    
In normal javascript code a newline can seperate two commands. Bookmarklets are in one line so substitute the linebreak with a semicolon ; what does it mean.. can u elaborate more?? –  vishal Jan 8 '13 at 13:36
1  
I recommend inserting semicolons always, although you can read more about it here. –  Karan Jan 8 '13 at 18:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.