Reputation: 53876
I have a rest service which accepts a URL parameter: myService/add?pageUrl=www.google.com
I'm trying to add this as a bookmark so whenever the user clicks the bookmark the current page URL is sent to my rest service.
Here is the javascript code I am using but the service is not being invoked
javascript:function savePage() {
window.open('myService/add?pageUrl=' + document.URL)
alert('Page saved');
}
savePage();
I receive the error:
Uncaught SyntaxError: Unexpected identifier
How can the above javascript be amended to so that when I add the code to a bookmark my service is invoked and the current URL the user is visiting is sent to the service?
Upvotes: 1
Views: 805
Reputation: 72947
Create bookmarklets like this:
javascript:(function(){/* your code here */})();
So, in your case:
javascript:(function(){window.open('myService/add?pageUrl='+document.URL)})();
After the javascript:
section, you have a self-executing (anonymous) function:
(function(){})()
And code in there will be evaluated.
Also, make sure you're not missing any semicolons (;
). Those will break your code in bookmarklets.
Upvotes: 6
Reputation: 86260
Missing a semicolon.
javascript:function savePage(){ window.open('myService/add?pageUrl='+document.URL
);
alert('Page saved');
} savePage();
Upvotes: 1