Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - jQuery to identify URL and append parameters

This jQuery code will run as a bookmarklet in the wordpress editor. The bookmarklet / jQuery code will edit the text in the content field and append a parameter (ex. ?parameter) to the end of the URL.

The URL is always the same domain name (ex. domain.com). However the URL will often contain directories (ex. domain.com/directory/index.html?parameter). I need the jQuery code to append regardless of what is after the domain name.

The most complex situation would be domain.com/directory/index.html?someotherparameter?parameter. This content area will often contain more than one url so the script will need to loop through the entire text.

My semi-working code

var url= 'domain.com';
var append = ' ?parameter ';

$(".wp-editor-area").each(
    function(){
        var haystack = $(this).text();
        $(this).text(haystack.replace(url, url+ append));
    });

The HTML code that it is modifying

<div id="wp-content-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content">&lt;a title="This is a hyperlink" href="http://domain.com/directory"&gt;This is a hyperlink&lt;/a&gt;</textarea></div>

its current output

<a title="This is a hyperlink" href="http://domain.com ?parameter /directory">This is a hyperlink</a>

Jsfiddle for convenience

Notice my output does not append after the entire URL. How can I modify this for more complex URLs and loop through a document if it had more URls in the body of text?

The only relevant and similar question I could find is this, however I could not duplicate the results.

Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, the Fiddle.

Your example code was doing exactly what you asked it to. It replaced the 'domain.com' portion of the the string with the domain and your parameters. Once it found 'domain.com' it stopped looking to see if there was anything else attached to it.

Instead of a straight text replace, try using a regular expression to find URLs within a string.

Source:

<div id="wp-content-editor-container" class="wp-editor-container">
    <textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content">
        &lt;a title="This is a hyperlink" href="http://domain.com/directory"&gt;This is a hyperlink&lt;/a&gt;
        &lt;a title="This is another hyperlink" href="http://google.com/directory"&gt;This is another hyperlink&lt;/a&gt;        
    </textarea>
</div>?

Javascript:

var url = 'domain.com';
var append = '?parameter ';

$(".wp-editor-area").each(function() {
    $(this).text(urlify($(this).text()));
});

function urlify(text) {
    var urlRegex = /((https?|ftp|file)://[domain.com][-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig;
    return text.replace(urlRegex, function(url) {
        return url + append;
    })
}?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...