user3344186
user3344186

Reputation: 191

Pass values from the main window to another window

in my script the user enters text in the main window, then using the control that I have attached below, the text entered in the first window (by the user) is transferred to the second window. In the second window, the text entered by the user must blink. My problem is: how do I make blink the text in the second window?

if (currentElementContent.length)   
{         
    var newWindow = window.open("ex.html","Nuova finestra","width=300,height=300 ,toolbar=yes, location=no,status=yes,menubar=yes,scrollbars=no,resizable=no");                 
    newWindow.document.write(currentElementContent);    
}

Upvotes: 0

Views: 77

Answers (1)

Marco Acierno
Marco Acierno

Reputation: 14847

How to pass data to another window

A great way is to use postMessage API which works in most recent browsers (i tested Chrome right now.)

newWindow.postMessage("The string you want to pass", "*");

"The string you want to pass": Is the object you want to pass, all browsers support for now only Strings.

*: Is the origin; from who is this message?

In the other window register for a message event.

function listener(event)
{
    // e.origin => origin
    // e.data => is the object (string) passed to it

    document.getElementById('testi').innerHTML += event.data + "<br/>";
}

if (window.addEventListener)
{
    window.addEventListener("message", listener, false);    
}
else
{
    attachEvent("message", listener);
}

event.data: Is the string which has been passed

event.origin: Is the origin of the message

attachEvent is used by IE.


How to let blink the text JSFiddle

I'm pretty sure jQuery can do it for you in a better way but anyway, to let your text blink you could make it visible/invisible every X milliseconds using a timer (interval).

In your ex.html start an interval which every 250 ms (or less, or more) will change the visiblity of a span (or div if you want or everything you want)

Example:

<span id="text">Text</span>

Then the script would be

var text = document.getElementById('text');
var isVisible = true;
setInterval(function()
{
  if (isVisible) {
    text.style.display = "none";
  } else {
    text.style.display = "";
  }
  isVisible = !isVisible;
}, 250);

setInterval(function, repeat)

function: Is the code block which will be executed every repeat milliseconds.

repeat: Is the milliseconds of wait between the two executions.

A working example: JSFiddle

Upvotes: 1

Related Questions