...

View Full Version : Need to store bgcolor in external JS file



NiteOwl
10-30-2006, 05:26 AM
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


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


The table need to look something like this:


<table width="100%" border="1" cellspacing="10" cellpadding="5" bgcolor="COLOR1" bordercolor="COLOR2">
<tr>
<td bgcolor="COLOR3">

</td>
</tr>
</table>


Thanks for looking...

brandonH
10-30-2006, 03:16 PM
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.

NiteOwl
10-30-2006, 04:22 PM
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
:)

brandonH
10-30-2006, 04:40 PM
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.

brandonH
10-30-2006, 04:51 PM
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=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.

NiteOwl
10-30-2006, 04:57 PM
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

VortexCortex
10-30-2006, 06:53 PM
The HTML file:


<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:


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.

brandonH
10-30-2006, 06:55 PM
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>

brandonH
10-30-2006, 07:22 PM
theres is another option to it.
that is if you want to write table into the document.


<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
10-30-2006, 07:38 PM
Thank you both.

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

:thumbsup:

NiteOwl
10-30-2006, 08:01 PM
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.


theres is another option to it.
that is if you want to write table into the document.


<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>

brandonH
10-30-2006, 10:20 PM
oops left out some quotes....



<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
10-31-2006, 12:02 AM
Got it...
Changed 1 thing

Thanks for all your help...




<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum