/    Sign up×
Community /Pin to ProfileBookmark

Pls help: Position elements on HTML pg using functions, external .js, .css files.

Hours of troubleshooting & searching dev sites on web & still stuck. Help much appreciated. Sorry for length but complex question ? am trying to be clear.

I?m trying to write javascript nav buttons to a specific place on html pages using an external .js file with multiple functions and a CSS level 2 (using ‘position’) style sheet.

Can get functions in external .js file to preload images and write HTML to document using the ?onload? method, but style sheet will not execute correct position or other attributes. I did find info on the web that ?it is possible to specify exactly where an element will be placed using DHTML and CSS positioning?, but not how.

Q. Am I just torturing myself? Is it worth it? Seems this is a nice efficient way to re-use navigation on multiple pages.
Q. Should I put a function in the <div class=?nav?> tag in the .html file or should the <div> tag go in the function in the external .js file?
Q. Is there a concept I?m missing here?
Q. Not sure where the document.close() should go when I?m using more than one function- one for each document.write?
Q. If there should be only one document.write() for both nav and header <div> tag sets, what is the proper syntax for the HTML in the ()? Do spaces mess up the JavaScript?

BEFORE the JavaScript function for the navigation html is added, the following is true:

  • 1. All elements appear on page in normal flow

  • 2. External .css file works, including positioning

  • 3. External .js file works with navigation images preload function (JavaScript)
  • AFTER a JavaScript function with document.write() is added, the following is true:

  • 1. I am using the document.close() method as per JavaScript Bible
    I used <body onload=”preloadimg();loadnav()”> in the HTML document, naming each function to avoid script conflict :

  • 2. All elements except what?s in the document.write() statement disappear (with or without document.close)

  • 3. Style sheet class attributes do not execute even when included in .html file (not linked)
  • STYLE SHEET
    Here is the class definition from my external style sheet:
    div.nav {position: absolute; top: 0px; left: 0px; width: 160px; height: 348px;}
    div.header {position: absolute; top: 0px; left: 160px; width: 620px; height: 88px;}

    FUNCTIONS
    Function to load navigation and header in div tags according to style sheet:

    function loadnav(){
    document.write(‘<div class=”nav”>Navigation stuff goes here</div>’)
    }

    function loadheader(){
    document.write(‘<div class=”header”>Header stuff goes here ? graphic continuation of navigation (no rollovers) placed at top of page to right of nav bar.</div>’)
    }
    document.close()

    Function to preload images for navigation rollovers (deleted much of code to spare you the details as I know this works):
    function preloadimg(){
    if (document.images)
    {
    //IMAGE OBJECTS FOR ACTIVE IMAGES
    var img1on = new Image();
    img1on.src = “images/navHome160x33_on.gif”;

    etc.

    //IMAGE OBJECTS FOR INACTIVE IMAGES
    var img1off = new Image();
    img1off.src = “images/navHome160x33_off.gif”;
    }

    //The else statement below sets the value of these “dummy” variables to an empty string so older browsers will know how to interpret the code.

    else
    {
    img1on = “”
    etc.
    img1off = “”
    etc.
    document.img1 = “”
    etc.
    }
    //FUNCTION TO ACTIVATE AN IMAGE ONMOUSEOVER
    function imgOn(imgName){
    if (document.images){
    document.images[imgName].src = eval(imgName + “on.src”);
    }
    }
    //FUNCTION TO ACTIVATE AN IMAGE ONMOUSEOUT
    function imgOff(imgName){
    if (document.images){
    document.images[imgName].src = eval(imgName + “off.src”);
    }
    }

    }

    [upl-file uuid=b73422cd-47b0-45a8-bb05-8a6dfb79af3c size=2kB]indextestallfunctions2.txt[/upl-file]

    to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @VladdyMay 17.2003 — While what you are trying to achieve is certainly doable (and you are making a few mistakes there which I can explain if you insist), your approach of creating navigation part of your page using javascript is wrong. By doing it you make your page unaccessible by all visitors who do not enable javascript in their browsers or use browsers incapable to run your script.

    If you are trying to save time by not having to change every page every time you change menu, I suggest you look at other technologies like SSI. Or there are web authoring tools that can do batch updates.
    Copy linkTweet thisAlerts:
    @M_RotchfordauthorMay 17.2003 — Vladdy, Dave,

    Thank you for your speedy replies and suggestions. For now I will retreat to the more traditional in-page JavaScript and plan to code another site for non-JavaScript browsers. I'll also look into SSI.

    Vladdy, if you would care to expound, I'd be most grateful to know other instances where my logic or methods failed. I'd love to see this work!

    Dave, I wasn't clear on the document.write point but am now.

    Thanks,

    Mary
    ×

    Success!

    Help @M_Rotchford spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...