0

it'll be undefined because the alert('outside '...) is being called before the requestValue callback. It's asyncronous so the alert will not wait for requestValue to finish before it executes.

You could either call a function within your callback which has that alert inside it or set up PubSub.

Method within callback:

remoteStorage.requestValue(stringdepuntos, function(key, value){
    alert("The value for '" + key + "' is '" + value + "'");
    this.val = value;
    someFunction();
});

function someFunction() {
    alert("outside " + remoteStorage.val);
}

but if you're doing that you may as well remove the this.val = value; line and just do this:

remoteStorage.requestValue(stringdepuntos, function(key, value){
    alert("The value for '" + key + "' is '" + value + "'");
    someFunction(value);
});

function someFunction(value) {
    alert("outside " + value);
}

Or simple pubsub:

<script type="text/javascript">

    var pubsub = (function() {
        var cache = {};

        function emit() {
            var e = arguments[0], 
                opts = Array.prototype.slice.call(arguments, 1),
                len = cache[e] ? cache[e].length : false, i = 0;

            for (i;i<len;i++) {
                 cache[e][i].apply(this, opts);
            }
        }

        function listen(e, callback) {
            cache[e] = cache[e] || [];
            // push callback onto subscription
            cache[e].push(callback);
        }

        return {
            emit: emit,
            listen: listen               
        };
    }());


    /*
     * Copyright 2010 Nicholas C. Zakas. All rights reserved.
     * BSD Licensed.
     */

    function CrossDomainStorage(origin, path){
        this.origin = origin;
        this.path = path;
        this._iframe = null;
        this._iframeReady = false;
        this._queue = [];
        this._requests = {};
        this._id = 0;
    }

    CrossDomainStorage.prototype = {
        //restore constructor
        constructor: CrossDomainStorage,
        //public interface methods
        init: function(){
            var that = this;
            if (!this._iframe){
                if (window.postMessage && window.JSON && window.localStorage){
                    this._iframe = document.createElement("iframe");
                    this._iframe.style.cssText = "position:absolute;width:1px;height:1px;left:-9999px;";
                    document.body.appendChild(this._iframe);
                    if (window.addEventListener){
                        this._iframe.addEventListener("load", function(){ that._iframeLoaded(); }, false);
                        window.addEventListener("message", function(event){ that._handleMessage(event); }, false);
                    } else if (this._iframe.attachEvent){
                        this._iframe.attachEvent("onload", function(){ that._iframeLoaded(); }, false);
                        window.attachEvent("onmessage", function(event){ that._handleMessage(event); });
                    }
                } else {
                    throw new Error("Unsupported browser.");
                }
            }
            this._iframe.src = this.origin + this.path;
        },

        requestValue: function(key, callback){
            var request = {
                    key: key,
                    id: ++this._id
                },
                data = {
                    request: request,
                    callback: callback
                };
            if (this._iframeReady){
                this._sendRequest(data);
            } else {
                this._queue.push(data);
            }   
            if (!this._iframe){
                this.init();
            }
        },

        //private methods
        _sendRequest: function(data){
            this._requests[data.request.id] = data;
            this._iframe.contentWindow.postMessage(JSON.stringify(data.request), this.origin);
        },

        _iframeLoaded: function(){
            this._iframeReady = true;
            if (this._queue.length){
                for (var i=0, len=this._queue.length; i < len; i++){
                    this._sendRequest(this._queue[i]);
                }
                this._queue = [];
            }
        },

        _handleMessage: function(event){
            if (event.origin == this.origin){
                var data = JSON.parse(event.data);
                this._requests[data.id].callback.call(this, data.key, data.value);
                pubsub.emit('/crossdomainstorage/finish', data.key, data.value);
                delete this._requests[data.id];
            }
        }
    };

    var remoteStorage = new CrossDomainStorage("http://somewhere.com", "/server.html");

    remoteStorage.requestValue(stringdepuntos, function(key, value){
        alert("The value for '" + key + "' is '" + value + "'");
    });

    pubsub.listen('/crossdomainstorage/finish', function(key, value) {
        alert("outside " + value);
    });
</script>
0

Thank you for your help :)

I tried the first method "method within callback" such as this:

var remoteStorage = new CrossDomainStorage("http://someher.com", "/server.html");


remoteStorage.requestValue(stringdepuntos, function(key, value){
    this.val = value;
    remoteStorage.val=value;
    somefunction(value);
/*1*/   alert ("inside one function " + value);
});



            function somefunction(value)
            {
    /*2*/       alert ("inside somefunction start function " + value);
                hero<?php echo $_SESSION['countforfor']; ?> = value;
                document.getElementById("fotop<?php echo $_SESSION['countforfor']; ?>").src=hero<?php echo $_SESSION['countforfor']; ?>;
                document.getElementById("fotog<?php echo $_SESSION['countforfor']; ?>").src=hero<?php echo $_SESSION['countforfor']; ?>;
        /*3*/       alert ("inside somefunction end function " + value);

}

This is with 2 values. As you can see there are a total of 3 alerts. When I use this the following happens:

9 alert popsup turn up (triple the amount of the ones in my code)
Now here is the popups in the order I see them and that I press intro to (As you can see Ive put a 1 2 and 3 next to them to make it easier):

2 (no value) (this is suppose to have the last value)
3 (the last value I entered appears)
1 (the last value I entered appears)
2 (the last value I entered appears)
3 (the last value I entered appears)
1 (the last value I entered appears)
2 (the last value I entered appears)
3 (the FIRST value I entered appears)
1 (the first value I entered appears)

Im going to attempt the pubsub method which I have even less clue what it is compared to that callback stuff

0

pubsub method gives same results.........As a matter of fact, identical I believe....

0

This line:

this._requests[data.id].callback.call(this, data.key, data.value);

is the one giving all the problems

0

To confirm the code is:

  remoteStorage.requestValue(stringdepuntos, function(key, value){

    this.val = value;
    remoteStorage.val=value;
    somefunction(value);

});




            function somefunction(value)
            {
            alert ("inside somefunction start function " + value);
                hero<?php echo $_SESSION['countforfor']; ?> = value;
                document.getElementById("fotop<?php echo $_SESSION['countforfor']; ?>").src=hero<?php echo $_SESSION['countforfor']; ?>;
                document.getElementById("fotog<?php echo $_SESSION['countforfor']; ?>").src=hero<?php echo $_SESSION['countforfor']; ?>;
                alert ("inside somefunction end function " + value);

            }       
0

If you're calling a function within the callback or using pubsub you don't need that .call() anymore. I'm not sure why that would be causing issues but might be worth putting that line of code back to how it was just to make sure we're not introducing more problems.

Your original question was how to grab the value outside of the callback which is what my two solutions above were for (they do the same thing but in different ways). The pubsub way is better because you are not restricted to adding everything you want to do into that someFunction() (you can add multiple pubsub.listen's).

The next problem you are describing (alerts appearing 9 times) is a completely separate issue. The _handleMessage() method must be getting called more than once in order for it to trigger the callback more than once so you need to figure out why/how/where that is happening.

The next issue with the values appearing incorrectly is beyond me as I don't know what you're talking about haha. You said it shows the first and last values you entered but I have no idea what values you're entering or where you're entering them....

0

Made the line exactly like it was in the beginning and now it just shows the second value.

This semiworks because with one alert line it pops up three times for two different values (that was a tongue twister....)

Yup, Im detecting that the problem isnt with the code you guys put (thanks again) but of the implementation of the function that I got off the site. I was hoping maybe someone could see it.

Yeah, its kind of difficult to describe without showing the page itself as its PHTML/JS. If you dont mind and have 15 minutes, I could show you thru TV/VNC as you have been helpful, like the rest, giving ideas.

I am thinking this is more and more related to how it was implanted. The article was written in 2010 and it is 2012 so maybe something has changed. All these test and its results are being conducted under Firefox (latest version). I have not even begun to think how it will act in other browsers....

Maybe explaining it would be better so if anyone has 15 minutes (even less, its nothing too complicated) for a VNC/TV session, please PM. This is enough to drive anyone nuts (more so, if Daniweb's posting system is horrible)

0

The funny thing is that a couple of days ago, instead of the value (which is a base64 image, I believe it is called) I decided to change it to a simply cookie, with is alot easier. That doesnt work either.....

0

Ill try to explain my code a bit to see if that helps:

First I have a canvas object which is basically my product. I draw different points at coordenates such as X:10 Y:7 X:8 Y: 10 etc..........When Im done, I use a function to convert that canvas into Data URI and I store it using localStorage. I do this similar to this:

localStorage.setItem("X10Y7X8Y10","data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="

As you see I strip it of ":" and " " just to make sure the browser doesnt interpret it in some weird way.

I do this for each product I want. When I go to the cart, the coordenates are passed as well and I restrip them again. I search for the coordenates in localStorage and the image is displayed. Once again this works perfectly in local but not online, which is what pisses me off.

0

Ill try to explain my code a bit to see if that helps:

First I have a canvas object which is basically my product. I draw different points at coordenates such as X:10 Y:7 X:8 Y: 10 etc..........When Im done, I use a function to convert that canvas into Data URI and I store it using localStorage. I do this similar to this:

var something="X10Y7X8Y10";
localStorage.setItem(something,"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==");

As you see I strip it of ":" and " " just to make sure the browser doesnt interpret it in some weird way.

I do this for each product I want. When I go to the cart, the coordenates are passed as well and I restrip them again. I search for the coordenates in localStorage and the image is displayed. Once again this works perfectly in local but not online, which is what pisses me off.

Edited by riahc3

0

I thought of a dirty hack but Im not sure if it is possible

Make a blank alert appear and then disppear. This would remove the alert from being shown and would make this finally work.

0

Well, it seems that Ill have to put a alert box even though it looks horrible...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.