FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

HELP with Javascript!

 


Satori
So here I am learning Javascript. I have used it to create some very basic window.open commands for thumbnail images and the like. Now I am trying to create something slightly more complex.

On my site, I have javascript commands to open a window to a certain size when someone clicks a thumbnail. What I want now is for not only the picture to be opened in a new window, but also a title, description, and eventually a shopping cart button. I want all of this to be done with javascript so I don't have to write a seperate page for each image on the site (as there are plenty of them.)

Please note, all of my javascript is contained in a seperate file on the site with a .js extension. I'm not sure if that causes any problems, but as far as I understand, it shouldn't.

Here's what I tried:

I set up each image name as part of an array:

Code:
var getround=new Array()
getround[1]="IMG_1236.jpg"
getround[2]="IMG_1238.jpg"
etc...


I have also set up the text I want for the description in a seperate array (with numbers to match the images in the previous array):

Code:
var roundtext=new Array()
roundtext[1]="blah blah blah"
roundtext[2]="blah blah blah"
etc...


Then I write a function to open a new window and pass along the info:

Code:
function displayround(x)
{
name="Round Pendant "+x
dir="gallery/pendants/round/"+getround[x]
text="<h1>"+name+"</h1><p>"+roundtext[x]+"</p><img src="+dir+" />"
window.open("http://www.satoriglass.com/gallery/imgdisplay.html", "_blank", "width=450, height=650, directories=0, location=0, resizable=0, toolbar=0")
return text
}


I then call the code when the user clicks on the thumbnail image:

Code:
<img src="gallery/pendants/round/tn_IMG_1236.jpg" onClick="displayround(1)" alt="" border="0" align="center" />


And the new window that opens (imgdisplay.html) has this code actually placed within it...it is not in the external .js file:

Code:
<script language="javascript">
<!-- Hide the script from old browsers --

   document.write(text)

//-->
</script>


Now, obviously this isn't working, or I wouldn't be posting. This is just what I've put together with my very slim knowledge of javascript. All that happens when the thumbnail is clicked is the window is opened (properly sized and all) but containing nothing. Just a blank window. Somehow, my information is not getting passed from one page to other and I don't know how to make it work.

Someone please help!
Cibes
The problem is that your variable on the site where you're opening the window is not available in the new window.
In order to use your variables you have to write into the new window from your first file:
Code:
  var text="testtext";
  var win = window.open("", "_blank", "width=450, height=650, directories=0, location=0, resizable=0, toolbar=0");
  win.document.write(text);

The "window.open()" function gives you the handler for the created window, using this you can write into it from your old site. You dont even need a second file anymore!
I hope I understood your problem and this helps you!
Satori
Cibes wrote:
The problem is that your variable on the site where you're opening the window is not available in the new window.
In order to use your variables you have to write into the new window from your first file:
Code:
  var text="testtext";
  var win = window.open("", "_blank", "width=450, height=650, directories=0, location=0, resizable=0, toolbar=0");
  win.document.write(text);

The "window.open()" function gives you the handler for the created window, using this you can write into it from your old site. You dont even need a second file anymore!
I hope I understood your problem and this helps you!

Cibes - Thank you SOOOOO MUCH!!! This fixes my problem completely! It's neat that I don't even need the html file! Now I just have to add all the html to the javascript function, which isn't such a big deal. I'll just create a couple variables that hold the main portion of the html and stuff the info that changes (the title, picture, description, and shopping cart buttons) in between the html containing variables.

Yay! Now I just have to figure out shopping carts and I'm set Smile Haha! It's going to take me a while to figure out CRE Loaded...but I'm sure I will at some point. The first step I suppose is to upload it!

Thanks again!!
Cibes
I'm glad I could help you!
Sometimes the solution that is working even looks better than the bugged ones Wink
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.