简体   繁体   English

如何拆分函数变量并在以后使用?

[英]How to split a function variable and use it later on?

I'm working on a hash system for a website I'm making, although some of the pages I want to hash require a PHP id at the end. 我正在为我正在制作的网站开发一个哈希系统,尽管我要哈希的某些页面最后需要一个PHP id。 I don't want to have .php in the hash url so I'm trying to split it so I can insert it later on. 我不想在散列网址中包含.php,因此我尝试将其拆分,以便稍后再插入。

An example of what is on the html side. html方面的示例。

<a href="#!/_radio/profile?id=3">link</a>

This is what I have so far. 到目前为止,这就是我所拥有的。 It all works except the extra variable isn't included at the end. 除了结尾处不包含多余的变量外,所有其他方法均有效。

var Radio = {};

   Radio = {

_jsInit: function () {

    $(window).bind('hashchange', function () {
        Radio._currentPage();
        Radio.loadPage(Radio._PageName, Radio._extra)
    });
    $(window).trigger("hashchange")

},

_currentPage: function () {
    this._PageName = location.hash.replace('#!/', '');
    if (this._PageName == "") window.location = "#!/_radio/home";
    this.values = this._PageName.split("?");
    this._extra = this.values[1];
},

loadPage: function (page, extra) {
    $('#content').fadeOut(200).load('_files/_v2/_pages/' + page + '.php?' + extra + '', function () {
      $('#content').fadeIn(400)
    });
},

};

$(document).ready(function () {
   Radio._jsInit();
});

For this before and after: 为此,之前和之后:

Before: "#!/_radio/profile?id=2"
After: "_files/_v2/_pages/profile.php?id=2"

You can use a regex to find the parts of the string you want: 您可以使用正则表达式查找所需的字符串部分:

 function convertStr(str) { var matches = str.match(/(\\/[^\\/]+)(\\?.*$)/); return "_files/_v2/_pages" + matches[1] + ".php" + matches[2]; } // call the function and display the result in the snippet var result = convertStr("#!/_radio/profile?id=2"); document.write(result); 

Or, any even simpler version that uses .replace() : 或者,任何使用.replace()简单版本:

 function convertStr(str) { return str.replace(/(^.*)(\\/[^\\/]+)(\\?.*$)/, "_files/_v2/_pages$2.php$3"); } // call the function and display the result in the snippet var result = convertStr("#!/_radio/profile?id=2"); document.write(result); 

After playing around with the code jfriend00 provided, I got it working. 在使用提供的代码jfriend00之后,我开始工作了。

var Radio = {};
Radio = {

_jsInit: function () {

    $(window).bind('hashchange', function () {
        Radio._currentPage();
        Radio.loadPage(Radio._PageName)
    });
    $(window).trigger("hashchange")

},

_currentPage: function () {
    this._PageName = location.hash.replace('#!/', '');
    if (this._PageName == "") window.location = "#!/_Radio/home";
},

loadPage: function (page) {
  var matches = page.split("?");
  var load = "_files/_v2/_pages/" + matches[0] + ".php?" + matches[1];

    $("#content").fadeOut(200).load(load, function () {
      $("#content").fadeIn(400)
    });

},

};

$(document).ready(function () {
Radio._jsInit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将匿名函数初始化为变量并在以后使用? - How can I initial an anonymous function into a variable and use it later? 如何将表单中的值存储在变量中以供以后在函数中使用? - How to store the value from a form in a variable for later use in a function? Javascript - 如何使用变量分隔符进行拆分功能? - Javascript - How to use variable separator for split function? 如何将正则表达式分配给变量以供以后在字符串上使用? - How to assign regex to variable for later use on a string? 单击后如何将函数返回值分配给变量以供以后在javascript中使用? - How to assign the function return value to a variable after a click for later use in javascript? 如何存储变量的内容供以后在其他函数中使用 (Javascript) - How do I store the content of a variable for later use in other function (Javascript) 保存变量以备后用 - Saving Variable for later use 如何定义 function 以便稍后在 Jquery 中使用它 - how to define a function to use it later in Jquery 为什么在 function 中可以使用稍后声明的变量 - Why inside a function you can use a variable that's declared later 您如何将 getText 的结果存储在变量中以供以后在 Nightwatch 中使用? - How do you store the result of getText in a variable to use later on in Nightwatch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM