Results 1 to 36 of 36

Thread: creating var in javascript and then access in html code

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    creating var in javascript and then access in html code

    i am creating a bigg page that will have lots of images that will have the same url..my idea to turn the page size smaller is to put at the top of the page a var with the url of the images and then just put in the images 'src' tag the var , this way by each image i get like less 50 chars(in let's say 100 images i am gettin 5000 chars and it will be a lot more than that!)

    so how can i do that?

    i put at the top of the code:
    <script language="javascript">
    var myPic = "http://............."
    </script>

    and now

    <img src="what here?">
    \m/\m/

  2. #2
    You have to go back into javascript and use it to print it out.

    Code:
    <script language="javascript">
    document.write('<img src="' + myPic + '">');
    </script>

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    isnt there a easier way? doin that i gain nothing and only turns my code into something more complicated :S
    \m/\m/

  4. #4
    I don't think so... what's made in javacript is kept in javascript, I don't think there's a way just to "pop" the value of a javascript variable into HTML.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmmm then how to explain that like <body onload="javascript:myfunction()"? its not into javascript lol
    \m/\m/

  6. #6
    onload, onclick, onmouseover etc. are all javascript, they're javascript event's. Once x event is fired, then do y javascript.

    I was thinking, possibly you could do something like this.

    Code:
    <script language="javascript">
    function doimg(url, image)
    {
        image.src = 'http://my/url/prefix/' + url;
    }
    </script>
    
    <img onload="doimg('myimage.jpg', this)">
    Only if there was a "when_parsed" type thing in javascript, that might work...
    Last edited by Chroder; Sep 26th, 2003 at 11:03 PM.

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm ill check that


    tks anyways
    \m/\m/

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    the code u wrote doesnt work..i tried just like u've put and it damn dont work :S
    \m/\m/

  9. #9
    Member
    Join Date
    Jan 2003
    Posts
    44
    onload, onclick, onmouseover etc. are all javascript, they're javascript event's. Once x event is fired, then do y javascript
    Actually onload, onclick, onmouseover, etc. are all HTML attributes for binding a certain event with a script action, which is not necessarily written in JavaScript.
    the code u wrote doesnt work..i tried just like u've put and it damn dont work
    Note that some of the values in that script (e.g., URL and the image's file name) need to be customized.
    Code:
    <img src="" alt="" onload="this.src = 'http://www.site.com/path/pic'">

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by jeffmott
    Actually onload, onclick, onmouseover, etc. are all HTML attributes for binding a certain event with a script action, which is not necessarily written in JavaScript.Note that some of the values in that script (e.g., URL and the image's file name) need to be customized.
    Code:
    <img src="" alt="" onload="this.src = 'http://www.site.com/path/pic'">
    try the code u just typed by urself. it won't work.
    Last edited by PT Exorcist; Oct 6th, 2003 at 02:03 PM.
    \m/\m/

  11. #11
    Member
    Join Date
    Jan 2003
    Posts
    44
    hmm, indeed. this also explains why chroder's also did not work. the onload event is never triggered, probably because initially there is nothing to load. so we can fix this by putting the event handler in the body rather than the image itself.
    Code:
    <body onload="document.getElementById('ImgID').src = 'http://www.site.com/path/pic'">
    
    <img id="ImgID" src="" alt="">
    
    </body>

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yep working! tks!
    \m/\m/

  13. #13
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I just found out about another way of using a variable in JavaScript in the HTML code.

    HTML Code:
    <html>
    <head>
    <title>Using the JavaScript entities.</title>
    <script language="JavaScript"><!--
    linkTo="http://www.jaworski.com/javascript"
    // -->
    </script>
    </head>
    <body>
    <a href="&{linkTo};">Click here.</a>
    </body>
    </html>
    I haven't yet tried that part, but it's verbatim from "Mastering JavaScript" by James Jaworski (BPB Publication) Page 40.



    NOPE, IT AIN'T WORKIN' !!!

    .
    Last edited by honeybee; Oct 10th, 2003 at 04:53 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  14. #14
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by PT Exorcist
    yep working! tks!
    so you would rather put 100 images in the body tag instead of the img tag. I don't see the point as it doesn't gain you anything.

    Code:
    <body onload="document.getElementById('ImgID').src = 'http://www.site.com/path/pic'">
    
    <img id="ImgID" src="" alt="">
    
    </body>
    hmm 132 characters compared to 81 ???
    Code:
    <body>
    
    <img id="ImgID" src="http://www.site.com/path/pic" alt="">
    
    </body>
    seems the old fashion way is a lot better.

    but if all you want to do is break it down some then just do this

    <img id="ImgID" src="pic.jpg" alt="">

    but if they are on a different server than you would be hotlinking and most hosts don't like that. so if they are on your server than I don't see any reason to add the url.

  15. #15

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    lol

    all the images have the same url so i just have to create a var in javascript with the url and then just put the thing = to the var

    thats why i turn it a lot smaller......
    \m/\m/

  16. #16
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    LOL..... no, you didn't answer my question. are they on your server? and no you didn't make it smaller, I already proved that.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There's another problem: you loose all benefits you might gain from parallel downloading. All images are forced to load AFTER the main page, which means that the browser must wait until all HTML, stylesheets, external JavaScripts, images you don't load via JS, objects etc. are loaded before it can download the images.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why do all the images have the same URL anyway? Are those spacer gifs *stern look*.

    Bad boy, go and learn CSS
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmmm what could i do with css? the thing is my page is a file lister page so in front of each file it has a file/folder image..u see?
    \m/\m/

  20. #20
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    think you img in your sig is big enough

  21. #21

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    actually there are sigs a lot bigger than mine in this forum but if i really have to change it i will

    \m/\m/

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could get dir of spacer gifs if you have any.

    And you can set the list bullet icon to an image, so you don't need all those <img> tags.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    good ideia! that seems to work like a charm!
    \m/\m/

  24. #24

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    but how can i set to a whole table that every text in it to be with that bullet style? i dont want to have to put in each row a
    Code:
    <ul imagesrc="http://..............">
    <li>text</li>
    \m/\m/

  25. #25
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you could also use css to put the path to the image and just call a class to all 100 list items. and then you only use 1 url.

  26. #26
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    in between your head tags or in an external file (preferred way) add this


    ul { list-style-image: url("http://site.com/file.jpg") }

    then in the body

    <ul>
    <li>text</li>
    </ul>


    that is all.

  27. #27

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmmm i am not very good with html..how do i do that?
    i tried putting:
    Code:
    <script>
    ul { list-style-image: url("http://site.com/file.jpg") }
    </script>
    and then
    Code:
    <ul>
    <li>text</li>
    </ul>
    but that doesnt seem to work
    \m/\m/

  28. #28
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    that is because it is a style not a script.

    <style type="text/css">
    ul { list-style-image: url("http://site.com/file.jpg") }
    </style>

  29. #29

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmmmmm it was a typo, in the html page i typed style and not script..what u just typed doesnt work :S
    \m/\m/

  30. #30
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    did you change the url to match yours?

  31. #31

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah as with that "wrong" url it showed the normal bullets i though it wouldnt work even with my pic

    tks!
    \m/\m/

  32. #32

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    one more thing,
    it seems like the tables ignore that formatting, how do i put so they obey to it?
    \m/\m/

  33. #33
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    what tables ignore what formatting?

    seems like you need to read up on css

    go here and enjoy

    http://www.w3.org/TR/REC-CSS2/

  34. #34

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm the thing its known as a table "feature"(read it as bug) that if u apply some kind of style to the body of a page it wont affect the table, so how to do it?
    \m/\m/

  35. #35
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by PT Exorcist
    hmm the thing its known as a table "feature"(read it as bug) that if u apply some kind of style to the body of a page it wont affect the table, so how to do it?
    what?? I have no idea what you are talking about. you are not applying a style to the page but to a list. it has nothing to do with a table.

    can you show us what you are talking about?

  36. #36

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    edit: never mind..did it!

    tks a lot!
    \m/\m/

Posting Permissions

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



Click Here to Expand Forum to Full Width