Passing variables to a new javascript

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
Thrynir

Passing variables to a new javascript

Post by Thrynir » Mon Oct 14, 2013 1:51 pm

Hi,

I'm trying to pass variables from a javascript witch have some parameters to an another that will execute them, but i don't know how pass them.

Here an exemple :

First js :

answer = true;

answerContent = "My answer";

close = true;

iimPlay("CODE:URL GOTO=imacros://run/?m=second.js",60);


Second js :

if (answer){
iiset("ans",answerContent );
iimplay("answer.iim")
}

if (close){
iimplay("close.iim")
}


Have you an idea of how make it works ?


Regards,
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Passing variables to a new javascript

Post by Tom, Tech Support » Tue Oct 15, 2013 11:49 am

Hi Thrynir,

Please see my notes on this post.
Regards,

Tom, iMacros Support
Thrynir

Re: Passing variables to a new javascript

Post by Thrynir » Wed Oct 16, 2013 5:14 pm

Well, I found a solution, ugly, but it works, I just use the url.

I push all var with window.document.location.hash like

Code: Select all

window.document.location.hash = "&type=main" + "&sujet=" + encodeURIComponent(sujet) + "&sujetF=" + sujetF + "&tag=" + encodeURIComponent(tag) + "&texteNT=" + encodeURIComponent(texteNT) + "&pauseNT=" + pauseNT + "&file=" + file + "&inter=" + inter + "&statut=" + statut + "&nettoyer=" + nettoyer + "&categorie=" + encodeURIComponent(categorie) + "&relance=" + relance + "&tagR=" + tagR + "&ticket_fils=" + ticket_fils + "&ticket_parent=" + ticket_parent + "&cc=" + cc +"&commentaire=" + encodeURIComponent(commentaire) + "&commentaireP=" + commentaireP + "&reponse=" + encodeURIComponent(reponse) + "&reponseP=" + reponseP + "&voler=" + voler;
And then I launch my second script

Code: Select all

iimPlay("CODE:URL GOTO=imacros://run/?m=LIB%2FRT.js\n");
In the second script, I get all vars

Code: Select all

function getUrlVars() {
  var vars = {};
  var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
  });
  return vars;
}

vars = getUrlVars();

type = vars["type"];
sujet = decodeURIComponent(vars["sujet"]);
sujetF = (vars["sujetF"] === "true");
To summarize

To push a string, an int, a bool

Code: Select all

window.document.location.hash = "&string=" + encodeURIComponent(string) + "&int=" + int + "&bool=" + bool;
To get a string, an int, a bool

Code: Select all

function getUrlVars() {
  var vars = {};
  var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
  });
  return vars;
}

vars = getUrlVars();

string = decodeURIComponent(vars["string"]);
int = vars["int"];
bool = (vars["bool"] === "true");
Tom, Tech Support
Posts: 3572
Joined: Mon May 31, 2010 4:59 pm

Re: Passing variables to a new javascript

Post by Tom, Tech Support » Thu Oct 17, 2013 9:01 am

Clever! Thanks for posting your solution, I will link it to my other post.
Regards,

Tom, iMacros Support
jessiecatello
Posts: 1
Joined: Thu Jun 04, 2015 4:34 am

Re: Passing variables to a new javascript

Post by jessiecatello » Thu Jun 04, 2015 4:35 am

Thrynir, thank you so much! great answer!!!
Post Reply