Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #10
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both.

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


  • #11
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #12
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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>
    Last edited by brandonH; 10-30-2006 at 10:21 PM. Reason: fix
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #13
    New Coder
    Join Date
    Aug 2006
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •