javascript - how to deal with quotes in bookmarklets - Stack Overflow

this is a script i found for writing jQuery bookmarklets and i've added three lines of code to it.

this is a script i found for writing jQuery bookmarklets and i've added three lines of code to it. the problem is jQuery code has lots of quotes (for selectors) and as i have to put the bookmarklets in a href="javascript:code" everything gets messed up with the href's double quotes. here is what my code looks like, i tried to escape double quotes, in many ways, but none did work. is there a way to deal with this problem?

<a href="javascript:(function(){

// the minimum version of jQuery we want
var v = '1.3.2';

// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
    var done = false;
    var script = document.createElement('script');
    script.src = '/' + v + '/jquery.min.js';
    script.onload = script.onreadystatechange = function(){
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'plete')) {
            done = true;
            initMyBookmarklet();
        }
    };
    document.getElementsByTagName('head')[0].appendChild(script);
} else {
    initMyBookmarklet();
}

function initMyBookmarklet() {
    (window.myBookmarklet = function() {
        // your JavaScript code goes here!
        var loc=window.location;
        $('body').append('<form id=\'IDform\' action=\'http:/pourid.3eeweb/read.php\' method=\'post\' ><input name=\'url\' type=\'text\' value=\''+loc+'\' /></form>');
        $('#IDform').submit();
    })();
}

 })();">bookmarklet</a>

when i click on the bookmarklet link, firebug says: SyntaxError: missing } after function body
but if i run the javascript only(not using an html link) it runs fine.

this is a script i found for writing jQuery bookmarklets and i've added three lines of code to it. the problem is jQuery code has lots of quotes (for selectors) and as i have to put the bookmarklets in a href="javascript:code" everything gets messed up with the href's double quotes. here is what my code looks like, i tried to escape double quotes, in many ways, but none did work. is there a way to deal with this problem?

<a href="javascript:(function(){

// the minimum version of jQuery we want
var v = '1.3.2';

// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
    var done = false;
    var script = document.createElement('script');
    script.src = 'http://ajax.googleapis./ajax/libs/jquery/' + v + '/jquery.min.js';
    script.onload = script.onreadystatechange = function(){
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'plete')) {
            done = true;
            initMyBookmarklet();
        }
    };
    document.getElementsByTagName('head')[0].appendChild(script);
} else {
    initMyBookmarklet();
}

function initMyBookmarklet() {
    (window.myBookmarklet = function() {
        // your JavaScript code goes here!
        var loc=window.location;
        $('body').append('<form id=\'IDform\' action=\'http:/pourid.3eeweb./read.php\' method=\'post\' ><input name=\'url\' type=\'text\' value=\''+loc+'\' /></form>');
        $('#IDform').submit();
    })();
}

 })();">bookmarklet</a>

when i click on the bookmarklet link, firebug says: SyntaxError: missing } after function body
but if i run the javascript only(not using an html link) it runs fine.

Share Improve this question edited Jan 10, 2013 at 16:12 Pouria P asked Jan 10, 2013 at 14:14 Pouria PPouria P 5951 gold badge8 silver badges24 bronze badges 3
  • Easiest solution is to put the code in an external js file and have the bookmarklet add the file to the page. – epascarello Commented Jan 10, 2013 at 14:19
  • Do you actually have line breaks in the html code or is it just in here for pretty factor? – epascarello Commented Jan 10, 2013 at 14:21
  • In your current script, you don't have a problem with quotes in the bookmarklet since you are not using double quotes (used to delimit the HTML attribute) in your code. However, the problem is your ments and newlines. – MrWhite Commented Jan 10, 2013 at 14:28
Add a ment  | 

3 Answers 3

Reset to default 5

There are a couple ways of doing this, one is to HTML-escape the quotes; &quot; or &#34; for ", &#39; for '.

The other way, my preferred, is to enter the bookmarklet as a string in JavaScript and attach it to the <a> at load time, meaning you don't have any HTML-related problems with it and the browser can do all the encoding for you if you save it.

Also as sbmaxx pointed out, you may need to remove the // ments. This is because a URI is not expected to have any line breaks and therefore the ment would never end when put onto one line.

You can not have // ments or line breaks in the attribute. It needs to be one long string.

If you want ments, it needs to be block ments /*foo*/

It needs to look like

<a href="javascript:document.body.style.color='red';alert('no line breaks');void(0);">foo</a>

Just delete ments // in your code and it should work ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647940a4638083.html

相关推荐

  • javascript - how to deal with quotes in bookmarklets - Stack Overflow

    this is a script i found for writing jQuery bookmarklets and i've added three lines of code to it.

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信