Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-30-2006, 05:26 AM   PM User | #1
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Need to store bgcolor in external JS file

I need to create an external .JS in perl script, with a Var that holds the bgcolor of a table. I need it to be an external .JS file because i will be changeing the color values alot.

I dont want to generate the HTML, at this time, with perl.
Createing the .JS file is easy, what i need to know is:

1. What the .JS file needs to look like.
2. And how to assign the table bgcolor to the var stored in the .JS file.

The .JS file will need several Vars

Code:
var color1 = "#000000";
var color2 = "#000000";
var color3 = "#000000";
The table need to look something like this:

Code:
<table  width="100%" border="1" cellspacing="10" cellpadding="5" bgcolor="COLOR1" bordercolor="COLOR2">
	<tr>
		<td bgcolor="COLOR3">
			
		</td>
	</tr>
</table>
Thanks for looking...
NiteOwl is offline   Reply With Quote
Old 10-30-2006, 03:16 PM   PM User | #2
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
the only way you can "store" a var in an external script is if the window using that script does not close. if you close the window the .js file goes back to what it was originally. I think what you are wanting is for the var to get set and saved to the .js file then later with any other window grab the value of the var that you set. unfortunately this cannot be done per say. you would have to give the user the ability to modify/create/write to the .js file. and really it would not be setting a var, it would be more like creating a whole new .js file. if you are running your own server and have a public folder that you dont mind letting people modify, then yes what you are asking for is, by workaround, possible. giving users that kind of control over contents of the folder though is asking for trouble. the only other option i can think of is if you have a server side script that will recreate the .js file. basically the window submits the bgcolor value to a script, then the script writes re-creates the.js file, changing only that var.


if i may, can i ask why you need to be able to do this. there may be a simpler way to go about it.
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 10-30-2006, 04:22 PM   PM User | #3
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Thanks for looking...
The .JS file will be created before the HTML file opens. It will not be changed while viewing the HTML.

In more Detail:
I have an application, 99% of it is written in perl.
In my demo, you can change the color scheme.

I am still using about a dozen HTML documents that support this app.
These, I would like to have the same color scheme as the rest of the generated pages.

Last night, I was able to do the same thing by createing a .CSS, when the color scheme was changed. The css link is hard coded in the HTML doc.

Problem solved...
I still would like the same solution in javascript.


Thanks
NiteOwl is offline   Reply With Quote
Old 10-30-2006, 04:40 PM   PM User | #4
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
you mean you want a javascript that will do the same as the script/program?
create/modify a .js file?

if so, look into FSO (File System Object)

i had a little script i was messing with a little while back that did this, but i think i deleted it cuz i wound up using VBscript. if i come across it, i'll post it.
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 10-30-2006, 04:51 PM   PM User | #5
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
http://www.pscode.com/vb/scripts/Sho...=5197&lngWId=2

has a good example of how to create a file with javascript.

unfortunately, you cannot actually modify the file. all you can do is read it line by line, read the whole thing, write a single line, or write the entire file.

basically read, write, and append. you wouldnt be able to just chage the value of the var. you would have to re write the entire .js file.
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 10-30-2006, 04:57 PM   PM User | #6
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
no..
Lets try again...

*************************************
(1)
I want to have the bgcolor of an html table to be a color stored in a external .JS file, as a var.

This is my guess...
.js file
var thecolor = "#000000";
*************************************
(2)
Now, how do i code the table to use the color in the .js file,
as the BGCOLOR?
*************************************




My original post says:
what i need to know is:
1. What the .JS file needs to look like.
2. And how to assign the table bgcolor to the var stored in the .JS file.



Thanks for looking Brandon
NiteOwl is offline   Reply With Quote
Old 10-30-2006, 06:53 PM   PM User | #7
VortexCortex
Regular Coder

 
Join Date: May 2005
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
VortexCortex is an unknown quantity at this point
You should stick to the CSS soluiton but...

The HTML file:
Code:
<html>
<head><title>Dynamic Background</title>
<script type="text/javascript" src="dynBg.js"></script>
</head>
<body>
	<table id="dyn_bg" border="1">
		<tbody>
			<tr>
				<td>test</td>
				<td>test</td>
			</tr>
			<tr>
				<td colspan="2">test data</td>
			</tr>
		</tbody>
	</table>
	<script type="text/javascript">setBg('dyn_bg');</script>
	<hr />
	<div id="anotherElement"> Same Background Color Here...</div>
	<script type="text/javascript">setBg('anotherElement');</script>
</body>
</html>
The "dynBg.js" file:
PHP Code:
var theColor "#ffff7f";
function 
setBg(id) {
    
document.getElementById(id).style.backgroundColor=theColor;

Be sure to write the .js file's HTTP header's in such a way that it doesn't get cached. "expires" = now for example... Do this for the CSS file if you keep it as well.
VortexCortex is offline   Reply With Quote
Old 10-30-2006, 06:55 PM   PM User | #8
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
ah ok gatcha now.

well as far as what the .js file needs to look like....

nothing special.

just create the file and write to it line by line

var color1="#000000";
var color2="#000000";
var color3="#000000";



do not put any script tags in the .js file. because it is a .js file it is alread know to be javascript.

then when trying to retrieve the values do as so:


inside the document (html)

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

document.body.style.backgroundColor=color1;
//or do as follows
document.getElementByid('elementsid').style.backgroundColor=color1;
</script>

<element id=elementsid></element>
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 10-30-2006, 07:22 PM   PM User | #9
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
theres is another option to it.
that is if you want to write table into the document.
Code:
<html>
<head>
<script type=text/javascript src="path to file.js">
var Tab="<table  width='100%' border='1' cellspacing='10' cellpadding='5' bgcolor='"+color1+" bordercolor='"+color2+"><tr><td bgcolor='"+color3+"></td></tr></table>";
</script>
</head>

<body>


some content...........

<script type=text/javascript>
document.write(Tab);
</script>


more content



</body>
</html>
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 10-30-2006, 07:38 PM   PM User | #10
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Thank you both.

I will try these out right away.
I am sure, 1 or all, is what i am looking for.

NiteOwl is offline   Reply With Quote
Old 10-30-2006, 08:01 PM   PM User | #11
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
This will be the best approch for me, but,
when i tested it, it did not write the table.

Thanks, i know this just needs minor changes to work.
Please revise.

Quote:
Originally Posted by brandonH View Post
theres is another option to it.
that is if you want to write table into the document.
Code:
<html>
<head>
<script type=text/javascript src="path to file.js">
var Tab="<table  width='100%' border='1' cellspacing='10' cellpadding='5' bgcolor='"+color1+" bordercolor='"+color2+"><tr><td bgcolor='"+color3+"></td></tr></table>";
</script>
</head>

<body>


some content...........

<script type=text/javascript>
document.write(Tab);
</script>


more content



</body>
</html>
NiteOwl is offline   Reply With Quote
Old 10-30-2006, 10:20 PM   PM User | #12
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
oops left out some quotes....

Code:
<html>
<head>
<script type=text/javascript src="path to file.js">
var Tab="<table  width='100%' border='1' cellspacing='10' cellpadding='5' bgcolor='"+color1+"' bordercolor='"+color2+"'><tr><td bgcolor='"+color3+"'></td></tr></table>";
</script>
</head>

<body>


some content...........

<script type=text/javascript>
document.write(Tab);
</script>


more content



</body>
</html>
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

Last edited by brandonH; 10-30-2006 at 10:21 PM.. Reason: fix
brandonH is offline   Reply With Quote
Old 10-31-2006, 12:02 AM   PM User | #13
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Got it...
Changed 1 thing

Thanks for all your help...


Code:
<script type=text/javascript src="dynBg.js"></script>


<script language="JavaScript">
<!-- ;
var Tab="<table  width='100%' border='1' cellspacing='10' cellpadding='5' bgcolor='"+color1+"' bordercolor='"+color2+"'><tr><td bgcolor='"+color3+"'>hello</td></tr></table>";
// end hide -->
</script>
NiteOwl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:16 PM.


Advertisement
Log in to turn off these ads.