Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Location
    Cheshire
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript loss of scope help needed

    Hi all,

    I'm having an issue trying to get the value returned from an easyXDM request (a url) into window.open(resulturl,'_blank');
    The window.open(); has to be part of the event handler for the button otherwise it doesn't work so it has to remain in the CLICK HANDLER or within the main part of the CLICKTHRU FUNCTION. It doesn't work if it's placed within the sub function remote.getClickURL.

    Any help would be massively appreciated.

    //CLICK HANDLER
    $("#btn_buynow").click(function(rv) {
    rv.stopPropagation();
    clickThru("BuyNow");
    });

    //CLICKTHRU FUNCTION
    function clickThru(clkname){
    var resulturl;
    remote.getClikURL(clkname, function(result){
    resulturl = result;
    //alert(result);//alerts correct e.g. Example Domain
    //alert(resulturl);//alerts correct e.g. Example Domain
    return resulturl;
    });
    //alert(result url);//alerts 'undefined'
    //window.open(resulturl,'_blank');//opens an empty blank window rather than the returned url
    }

    //REMOTE.GETCLICKURL FUNCTION
    var remote = new easyXDM.Rpc({
    local: "/name.html",
    swf: "/easyxdm.swf"
    },{
    remote: {
    getClikURL: {}
    },
    local: {
    alertMessage: function(msg){
    alert(msg);
    }
    }
    });

    Daz

  • #2
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many Many thanks for giving the information

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    //CLICK HANDLER
    $("#btn_buynow").click(function(rv) {
    rv.stopPropagation();
    //try like this
    alert(clickThru("BuyNow"));
    });

    //CLICKTHRU FUNCTION
    function clickThru(clkname){
    var resulturl;
    remote.getClikURL(clkname, function(result){
    resulturl = result;
    //alert(result);//alerts correct e.g. Example Domain
    //alert(resulturl);//alerts correct e.g. Example Domain
    return resulturl;
    });

    or make a global var ...
    var resulturl
    then change to this ...

    function clickThru(clkname){
    remote.getClikURL(clkname, function(result){
    resulturl = result;
    //alert(result);//alerts correct e.g. Example Domain
    //alert(resulturl);//alerts correct e.g. Example Domain
    return resulturl;
    });
    alert(resulturl);
    window.open(resulturl,'_blank')
    Last edited by DaveyErwin; 06-14-2014 at 04:41 PM.

  • #4
    New to the CF scene
    Join Date
    Jun 2014
    Location
    Cheshire
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Many Thanks - But Not The Solution

    Thanks for your input and rapid reply, it's very much appreciated however;
    if only it was that simple, this still spits out 'undefined' on first click

    This is exactly what I've tested from your reply:

    $("#btn_buynow").click(function(rv) {
    rv.stopPropagation();
    clickThru("BuyNow");
    });

    var resulturl;

    function clickThru(clkname){
    remote.getClikURL(clkname, function(result){
    resulturl = result;
    return resulturl;
    }
    alert(resulturl);//Returns 'undefined'
    //window.open(resulturl,'_blank');
    });

    I've also tested declaring the variable (var result url inside the clickThru function and combinations there of.

    The really strange thing about this is that if you click the button a second, third or forth time, 'alert' returns the correct 'resulturl',

    alert(resulturl); //Returns Correct - 'http://www.example.com'

    It's like it's getting stuck, and only feeds through to alert(resulturl); on consecutive clicks.

    Does that make sense?

    Daz

    P.S. Please remember the actual problem here is that window.open(resulturl,'_blank'); needs to receive the value.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •