最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to pass variable from one function to another, both with parameters using JavaScript - Stack Overflow

programmeradmin4浏览0评论

We have several landing pages with call-to-action buttons ("Download the report", for example) and need to report through a URL string two variables from the actual button click (report title and report type and whether the visitor who is clicking the button is a new or existing client. The analytics pany requires the final URL to be sent like this:

=" + assetName + "&assetType=" + assetType + "&custType=" + custType);

To gather the assetName and assetType, I've created this function, which has already been in production and works fine for reporting the assetName and assetType.

function tracker(assetName,assetType) {
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", "=" + assetName + "&assetType=" + assetType + "&custType=" + custType);
  if (typeof fileref!="undefined") {        
    document.getElementsByTagName("head")[0].appendChild(fileref);
  }

}

Now, we need to also track whether the client is "new" or "current", so I have this function:

function cStatus(elem) {
    var els = [];
    while (elem) {
        els.unshift(elem);
        elem = elem.parentNode;
        if (elem.id == "new") {
            break;
        } else if (elem.id == "current") {
            break;
        }
    }
    custType = elem.id;
}

To clarify, the call-to-action button is included on the page twice, once in the "new" div and the other in the "current" div.

<div id="new">
...button code...
</div>

and

<div id="current">
...button code...
</div>

In order to create that URL string for reporting, my thinking is to use the "cStatus" function to identify the div id ('new' or 'current'), then use the cStatus function to get the info on the button click. The button has an onclick that passes the 'assetName' and 'assetType':

onclick="tracker('PDF','document.pdf')

The problem I'm having is getting all three of those variables into one function, so I can create the URL string for reporting. I've search and found solutions for functions without parameters, but cannot figure out how to pass a variable from one function with parameters "function cStatus(elem)" to another function with parameters "tracker(assetName, assetType), so that it can ultimately be passed along in a URL string for reporting.

We have several landing pages with call-to-action buttons ("Download the report", for example) and need to report through a URL string two variables from the actual button click (report title and report type and whether the visitor who is clicking the button is a new or existing client. The analytics pany requires the final URL to be sent like this:

https://thisurl.html?assetName=" + assetName + "&assetType=" + assetType + "&custType=" + custType);

To gather the assetName and assetType, I've created this function, which has already been in production and works fine for reporting the assetName and assetType.

function tracker(assetName,assetType) {
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", "https://thisurl.html?assetName=" + assetName + "&assetType=" + assetType + "&custType=" + custType);
  if (typeof fileref!="undefined") {        
    document.getElementsByTagName("head")[0].appendChild(fileref);
  }

}

Now, we need to also track whether the client is "new" or "current", so I have this function:

function cStatus(elem) {
    var els = [];
    while (elem) {
        els.unshift(elem);
        elem = elem.parentNode;
        if (elem.id == "new") {
            break;
        } else if (elem.id == "current") {
            break;
        }
    }
    custType = elem.id;
}

To clarify, the call-to-action button is included on the page twice, once in the "new" div and the other in the "current" div.

<div id="new">
...button code...
</div>

and

<div id="current">
...button code...
</div>

In order to create that URL string for reporting, my thinking is to use the "cStatus" function to identify the div id ('new' or 'current'), then use the cStatus function to get the info on the button click. The button has an onclick that passes the 'assetName' and 'assetType':

onclick="tracker('PDF','document.pdf')

The problem I'm having is getting all three of those variables into one function, so I can create the URL string for reporting. I've search and found solutions for functions without parameters, but cannot figure out how to pass a variable from one function with parameters "function cStatus(elem)" to another function with parameters "tracker(assetName, assetType), so that it can ultimately be passed along in a URL string for reporting.

Share Improve this question edited Apr 17, 2015 at 13:11 user3120861 asked Apr 16, 2015 at 22:28 user3120861user3120861 1853 silver badges18 bronze badges 8
  • 5 Neither of those functions call the other. It doesn't make sense to talk about "passing" anything if there aren't any function calls involved. – Pointy Commented Apr 16, 2015 at 22:29
  • You could use Global Variables ..... to share information – Jose Ricardo Bustos M. Commented Apr 16, 2015 at 22:31
  • 3 Or even better: a closure. – Sebastian Simon Commented Apr 16, 2015 at 22:32
  • 2 Global variables are nearly always a bad idea, as they pollute the global namespace and give rise to the likelihood of naming clashes. – Mitya Commented Apr 16, 2015 at 22:33
  • Do you mean that the two functions are asynchronous with respect to each other - ie, they run in different event threads? – Roamer-1888 Commented Apr 16, 2015 at 22:33
 |  Show 3 more ments

2 Answers 2

Reset to default 4

Your question doesn't make to much sense, still, I guess you need a global variable, here's an example:

function func1(){
    window.myvar = "something";
}

Then get your global variable like this:

function func2(){
    var newvar = window.myvar + "append something";
}

Another option is to assign the variable outside the function scope, making it also global.

var myvar = "bla";
    
function func1(){
    myvar = "something";
}
     
function func2(){
    alert(myvar);
}

The value of myvar inside func2 is now something


And finally, make your variable Automatically Global by assigning a value to it before being declared.

function func1(){
    myvar = "something";
}

The above will make myvar as a global variable, even if it is executed inside a function.

Learn more about Scopes in JavaScript

You could call one function from the other passing the parameters along.

function func1(param2){
  //do stuff with param2
}

function func2(param1){
   func1(param1);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论