使用JavaScript解码URL参数

时间:2012-08-20 17:56:45

标签: javascript html forms decoding url-parameters

这应该是一项简单的任务,但我似乎无法找到解决方案。

我有一个基本字符串作为查询字符串参数传递,如下所示:This+is+a+message+with+spaces。我想使用JavaScript将该参数解码为This is a message with spaces,但我似乎无法将其解码。

我已尝试过decodeURI('This+is+a+message+with+spaces')但结果仍然包含+符号。

5 个答案:

答案 0 :(得分:50)

是的,decodeURIComponent函数不会将+转换为空格。所以你必须用替换函数替换+。

理想情况下,以下解决方案有效。

var str_name = 'This+is+a+message+with+spaces';
decodeURIComponent((str_name + '').replace(/\+/g, '%20'));

答案 1 :(得分:24)

就像已经指出的那样,decodeURI函数不会将+转换为空格, 但是 这里有一些值得实现的事情:
  • decodeURI旨在用于整个URI,即它不会解码?&=+等分隔符
  • 应使用
  • 解码参数decodeURIComponent (值得一看:What is the difference between decodeURIComponent and decodeURI?
  • 您尝试解码的
  • 字符串实际上可能包含编码为+的{​​{1}},因此您不应在转换后替换%2B,因为您可能会丢失+个符号你真的想要那里,例如+应成为:
    something?num=%2B632+905+123+4567
    因为您可能要提取数字:something?num=+632 905 123 4567

所以正确的方法是:

+632 905 123 4567

答案 2 :(得分:19)

加号未编码/解码。要查看解码函数的工作原理,您需要先传递编码的URI。看看:

encodeURI( "http://www.foo.com/bar?foo=foo bar jar" )

将生成:http://www.foo.com/bar?foo=foo%20bar%20jar,即编码的URI。

decodeURI( "http://www.foo.com/bar?foo=foo%20bar%20jar" )

将生成:http://www.foo.com/bar?foo=foo bar jar,即解码后的URI。

答案 3 :(得分:4)

下面的代码将解码并以对象的形式为您提供参数

    @objc func singleTap(_ tap : UITapGestureRecognizer) {
        let attributedText = NSMutableAttributedString(attributedString: self.attributedLabel!.attributedText!)
        attributedText.addAttributes([NSAttributedStringKey.font: self.attributedLabel!.font], range: NSMakeRange(0, (self.attributedLabel!.attributedText?.string.count)!))

        // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
        let layoutManager = NSLayoutManager()
        let textContainer = NSTextContainer(size: CGSize(width: (self.attributedLabel?.frame.width)!, height: (self.attributedLabel?.frame.height)!+100))
        let textStorage = NSTextStorage(attributedString: attributedText)

        // Configure layoutManager and textStorage
        layoutManager.addTextContainer(textContainer)
        textStorage.addLayoutManager(layoutManager)

        // Configure textContainer
        textContainer.lineFragmentPadding = 0.0
        textContainer.lineBreakMode = self.attributedLabel!.lineBreakMode
        textContainer.maximumNumberOfLines = self.attributedLabel!.numberOfLines
        let labelSize = self.attributedLabel!.bounds.size
        textContainer.size = labelSize

        let tapLocation = tap.location(in: self.attributedLabel)

        // get the index of character where user tapped
        let index = layoutManager.characterIndex(for: tapLocation, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

        if index > (self.attributedLabel.text?.count)! { return }
        var range : NSRange = NSRange()
        if let type = self.attributedLabel.attributedText?.attribute(NSAttributedStringKey(rawValue: "link"), at: index, effectiveRange: &range) as? String {

            // to get full text which was clicked
            print((self.attributedLabel.text! as NSString).substring(with: range))

            if type == "termsLink" {
                //.. do smth
                print("termsLink click")

            } else { //policyLink
                //.. do smth
                print("policyLink click")

            }
        }
    }

答案 4 :(得分:1)

我创建了自己的字符串方法来支持所需的编码/解码。这些方法将正确处理+编码和解码,允许您在字符串中使用加号(+)并仍然将原始空格编码为+。

String.prototype.plusEncode = function() {
    return encodeURIComponent(this).replace(/\%20/gm,"+");
}

String.prototype.plusDecode = function() {
    return decodeURIComponent(this.replace(/\+/gm,"%20"));
}