如何使用 mooTools 更改查询字符串的值?

发布于 2024-12-28 08:54:40 字数 283 浏览 1 评论 0 原文

给定一个包含带有查询字符串参数的绝对 URL 的字符串,使用 JavaScript 和/或 MooTools 替换参数值的最简单方法是什么?

例如

// Change this
'/foo/bar?frob=123456789'

// Into this
'/foo/bar?frob=abcdefg'

编辑

URL 不是当前窗口的位置。它是一个包含字符串的变量。我只是想操纵这个字符串。

Given a string containing an absolute URL with a querystring parameter, what is the easiest way to replace the value of the parameter using JavaScript and/or MooTools?

e.g.

// Change this
'/foo/bar?frob=123456789'

// Into this
'/foo/bar?frob=abcdefg'

EDIT

The URL is not the location of the current window. It is a variable containing a string. I just want to manipulate this string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2025-01-04 08:54:40

除了您所做的之外,这可能会更好,因为它将扩展 String 并消除对 URI 类的依赖...

(function() {

// deps on Types/String.toQueryString()
String.implement({

    fixQueryString: function(key, value) {
        var query = this.parseQueryString() || {};
        query[key] = value;
        return Object.toQueryString(query);
    }

});

// exports:
this.replaceUrlParameter = function(url, key, value) {
    var parts = url.split('?'), QS = parts[1] || null;

    return QS ? [parts[0], QS.fixQueryString(key, value)].join("?") : url;
};

})();

console.log(replaceUrlParameter('/foo/bar', 'frob', 'ownies'));
/foo/bar/
console.log(replaceUrlParameter('/foo/bar?frob=123123123', 'frob', 'ownies'));
/foo/bar?frob=ownies

http://jsfiddle.net/dimitar/fjj6W/

Further to what you did, this may be better as it will extend String and it removes dependency on URI class...

(function() {

// deps on Types/String.toQueryString()
String.implement({

    fixQueryString: function(key, value) {
        var query = this.parseQueryString() || {};
        query[key] = value;
        return Object.toQueryString(query);
    }

});

// exports:
this.replaceUrlParameter = function(url, key, value) {
    var parts = url.split('?'), QS = parts[1] || null;

    return QS ? [parts[0], QS.fixQueryString(key, value)].join("?") : url;
};

})();

console.log(replaceUrlParameter('/foo/bar', 'frob', 'ownies'));
/foo/bar/
console.log(replaceUrlParameter('/foo/bar?frob=123123123', 'frob', 'ownies'));
/foo/bar?frob=ownies

http://jsfiddle.net/dimitar/fjj6W/

独自唱情﹋歌 2025-01-04 08:54:40

很抱歉撞到了旧帖子,但我认为它已经在 mootools 中了。无需自己编写:-) http://mootools.net/docs/ more/Types/URI#URI:setData

或者,如果您使用的是非常旧版本的 mootools,请查看此处:http://pilon.nl/mootools/2009/02/05/extra-browsersextras/

Sorry for bumping an old post but I think it's already in mootools. No need to write it yourself :-) http://mootools.net/docs/more/Types/URI#URI:setData

Or take a look here if your on a very old version of mootools: http://pilon.nl/mootools/2009/02/05/extra-browsersextras/

留蓝 2025-01-04 08:54:40

如果您想在不重新加载页面的情况下执行此操作,则没有办法(这是网络浏览器安全模型的一部分)。
如果您不介意重新加载页面,请考虑:

location.href='http://example.com/foo/bar?frob=baz'

无需重新加载的替代方法是使用 location.hash,即将

location.hash = 'myhash'

url 更改为 http://example.com/foo/bar?frob=123456789#myhash

这是Hash bang url

If You want to do this without reloading the page, there is no way (this is part of the web browser security model).
If You don't mind reloading the page, consider:

location.href='http://example.com/foo/bar?frob=baz'

An alternative without reloading is using location.hash, i.e.

location.hash = 'myhash'

to change url to http://example.com/foo/bar?frob=123456789#myhash

This is the basis of the Hash bang urls

暮色兮凉城 2025-01-04 08:54:40

这是我想出的解决方案。这依赖于MooTools。

function replaceUrlParameter (url, parameterName, newValue) {
    var uri = new URI(url);
    var uriBase = ri.get('directory') + uri.get('file');
    var query = uri.get('query').parseQueryString(false, true);
    query[parameterName] = newValue;

    uriBase += '?';
    for (var i in query) {
        if (i === 'undefined') continue;
        if (i === '') continue;
        uriBase += i + '=' + escape(query[i]) + '&';
    }
    return uriBase.substring(0, galleryLinkBase.length - 1);
}

这是特定于我的用法的,但它可能会作为将来其他人的起点。

This is the solution I came up with. This is dependent on MooTools.

function replaceUrlParameter (url, parameterName, newValue) {
    var uri = new URI(url);
    var uriBase = ri.get('directory') + uri.get('file');
    var query = uri.get('query').parseQueryString(false, true);
    query[parameterName] = newValue;

    uriBase += '?';
    for (var i in query) {
        if (i === 'undefined') continue;
        if (i === '') continue;
        uriBase += i + '=' + escape(query[i]) + '&';
    }
    return uriBase.substring(0, galleryLinkBase.length - 1);
}

This is specific to my usage but it might serve as a starting point for others in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文