类型错误: $.url 不是一个函数

TypeError: $.url is not a function

本文关键字:一个 函数 错误 url 类型      更新时间:2023-09-26

我在app/views/reports下有一个名为_report_js.html.erb文件的javascript文件。当我运行我的应用程序时,我收到错误:

类型错误: $.url 不是函数

这是我的代码:

<script type="text/javascript">
    if($.url().param('ig') == 'true') {
      alert("hi");
      $('#icons').hide();
      $('#photos').show();
    end
</script>
$.url() 

不是 jQuery Core 的一部分。如果你想实现类似的东西,也许使用

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;
    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

要使用它:

var tech = getUrlParameter('technology');

在您的示例中,它将是这样的

if(getUrlParameter('ig') == 'true') {

//Just use this, no need to do loops:
    var getUrlParameter = function getUrlParameter(sParam) {
        var r = "";
        if (window.locaton.href.substring(window.locaton.href.indexOf(sParam + "="))) {
            r = window.locaton.href.substring(window.locaton.href.indexOf(sParam + "=")(sParam + "=").length + 1);
            // trim if there is other parameter on the list
            if (r.indexOf("&") > 0)
                r = r.substring(0, r.indexOf("&"));
        }
        // please do some simple test, I just wrote it without testing the code
        return r;
    }

您的代码可能假定库 purl 库 https://github.com/allmarkedup/purl 已加载,但未或无法访问。

您可以尝试另一种方法:

if(purl().param('ig') == 'true') {