jQuery识别URL并附加参数 [英] jQuery to identify URL and append parameters

查看:126
本文介绍了jQuery识别URL并附加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此jQuery代码将在wordpress编辑器中作为小书签运行.小书签/jQuery代码将编辑内容字段中的文本,并在URL末尾附加一个参数(例如?parameter).

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.

URL始终是相同的域名(例如domain.com).但是,URL通常包含目录(例如domain.com/directory/index.html?parameter).无论域名后面是什么,我都需要添加jQuery代码.

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.

最复杂的情​​况是domain.com/directory/index.html?someotherparameter?parameter.该内容区域通常会包含多个网址,因此脚本需要遍历整个文本.

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.

我的半工作代码

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

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

正在修改的HTML代码

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>

其当前输出

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

为方便起见,Jsfiddle

请注意,我的输出未附加在整个URL之后.如果文本正文中包含更多的UR,我该如何修改它以获取更复杂的URL并遍历整个文档?

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.

谢谢!

推荐答案

首先,小提琴

您的示例代码完全按照您的要求执行.它用域和您的参数替换了字符串的'domain.com'部分.找到'domain.com'后,它便停止寻找是否还有任何附加物.

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.

请尝试使用正则表达式来查找字符串中的URL,而不是使用纯文本替换.

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

来源:

<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 = /(\b(https?|ftp|file):\/\/[domain.com][-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(urlRegex, function(url) {
        return url + append;
    })
}​

这篇关于jQuery识别URL并附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆