...

View Full Version : pull a javascript variable from other page



sugar2
05-21-2005, 02:03 AM
hi
i have 2 pages, one have two javascript variables assigned, lets say...


PAGE 1:
<script>navnext = 1</script>
<script>navlen = 7</script>

PAGE2:
how can i pull the PAGE1 variable values?


note1: also my two pages are in the same domain.

note2: i know that the PASSING method using SUBMIT button works well in this cases, but the thing is that i wont pass the variable... i want to pull the variable...
only using javascript or DOM, no dinamic php either asp...

i tried to changing the context of the page like

<base href='http://mypage.domain/' target='_self'> with no results, also i need to call the same pair of variable of too many pages from the same page...

any javascrip or DOM idea?

thanks members

Mr J
05-21-2005, 07:47 AM
Please take a look at the following page(s) about tranferring data to another page, it may help you out.

www.huntingground.freeserve.co.uk/scripts/passdata/p_data1.htm

sugar2
05-21-2005, 05:24 PM
at this moment i will give it a read

thanks!!

sugar2
05-21-2005, 07:22 PM
ok, this is the panorama: i have a yah_oo-store, and i cannot use dynamic scripts inside orpduct pages, this time im doing a "Special Items page By Department", and it its a simple page with links to each page wich have special items, things become hard since, each page wich have special items have a link like : www.mypage.mydomain/department_page.html?pageNum=5, because i have a javascript dynamic pagination wich load only the items marked as "special" in the current department page
actually i have like options:

1- load the pages by iframes and then get the variable values (slown loading time)
2- create a template wich recalculate the variable values and pull it by a .js include, (daily upload)
3- recalculate the variable values inside the pulling page (hard to coding), no daily updates, once published is fast loading

thanks for the advising

JPM
05-21-2005, 07:55 PM
Dont think there is another way to pass variables with javascript than doing something like this

page1

<script>
var test = "somevalue";
document.location.href ="test.html?"+test;
</script>


page2

var test = document.location.href;
var i = test.indexOf("?");
test = test.substring(i+1,test.length);

alert(test);

Mr J
05-21-2005, 08:25 PM
Usually you would go

<script type="text/javascript">
<!--
if (location.search.length > 0)
dataPassed = unescape(location.search.substring(1));
alert(dataPassed)
//-->
</script>

in page 2

sugar2
05-21-2005, 08:34 PM
i cannot figureout how could it work...
i hope this way:

variable source page:
page name: test1.html

<script>
var test = "pageNum=4"; // this is the variable i need: pageNum
document.location.href ="test.html?"+test; // why change the location?
</script>


pulling page:
page name: test2.html

<script>
var test = document.location.href;
var i = test.indexOf("?"); // what is supposed to do this code?
test = test.substring(i+1,test.length);
alert(test); // print the current url...
</script>
it just prints the current url...


in this sample you provided me: have i to load first the source page, and then the calling page?, if yes, this is a passing variable code...

if no, then this is a pulling variable code...

wich one is?

thanks!

sugar2
05-21-2005, 08:45 PM
Usually you would go

<script type="text/javascript">
<!--
if (location.search.length > 0)
dataPassed = unescape(location.search.substring(1));
alert(dataPassed)
//-->
</script>

in page 2


since i working here with location.search i belive i have to load the first page before load the seccond one...

is there a way to call the variable value without load the page first?


thanks guys

Mr J
05-21-2005, 11:01 PM
Not with javascript

Eternity Angel
05-22-2005, 05:16 PM
If you want to load variables from any source, then shove them all in an external .js file, and call the file like so:


<script src="path/to/the/file.js" type="text/javascript"></script>

If you want to pass variables from one page to another, then use the DOM:

test.html:

<script type="text/javascript">

document.myVar = 32;

window.open('test2.html','some_name','');

</script>

test2.html:

<script type="text/javascript">

v = window.opener.document;

alert('I do declare that myVar is equal to '+v.myVar+'.');

</script>

sugar2
05-23-2005, 05:44 PM
thanks Eternity Angel, but i think Mr J has the reason, since its imposible to load like 30 new windows, only to load a page wich pull their 60 variables..

i think the solution is:

1- Put a simple link in the calling page, and in the link page put a javascript wich look if its page is called from certain page and then if yes, redirect to the proper URL, and if not, let it load.
i mean like that:
PSEUDOCODE:
<SCRIPT>

NO WORKING

if (document.referrer = "http://store.crafta.com/specials-by-department.html")
location.href = "?pageNum=5";

</SCRIPT>
or 2- Calculate the variables values inside the calling page. (i think this is the better and harder solution...

thanks everibody

Mr J
05-23-2005, 07:44 PM
If the variables do not change then you can do what Eternity Angel suggested.

sugar2
05-23-2005, 07:46 PM
i think is no good practice to open more than 1 pop window per page... and if i do that i should open more than 30 windows...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum