Closed Thread
Results 1 to 7 of 7

Thread: style.left with external css

  1. #1
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    style.left with external css

    I'm having problems with accessing the left value of an element if I've defined it in an external style sheet.

    There's no problem if I define the style within the HTML, but I don't really want to do that.

    So, if I have

    [highlight="HTML"]
    <div id="myDiv" style="position: relative; left: 150px;"></div>
    [/highlight]

    then
    [highlight="javascript"]
    alert(document.getElementById('myDiv').style.left) ;
    [/highlight]

    produces an alert showing "150px".

    However, if I use an external style sheet with
    [highlight="CSS"]
    #myDiv {
    position: relative;
    left: 150px;
    }
    [/highlight]

    and declare the div as
    [highlight="HTML"]
    <div id="myDiv"></div>
    [/highlight]

    then the same javascript line as before produces an empty alert. I'm guessing it's because the javascript can't access the external style sheet. Is there a way around this?

    If I have to include the style within the HTML then I guess I'll have to, but I'd much rather keep everything to do with styles in a seperate file.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: style.left with external css

    make sure you have included the CSS file in the head like:
    Code:
    <link href="mycss.css" rel="stylesheet" type="text/css" />
    btw: did you mean:
    Code:
    <div id="myDiv" style="position:relative; left:150px;"></div>
    instead of:
    Code:
    <div id="myDiv" style="position=relative; left=150px;"></div>
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  4. #3
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: style.left with external css

    Quote Originally Posted by amrosama View Post
    make sure you have included the CSS file in the head like:
    Code:
    <link href="mycss.css" rel="stylesheet" type="text/css" />
    yeah, the CSS file's been included and the page displays as expected. It's just when I try to access the left value through js that I have a problem.
    btw: did you mean:
    Code:
    <div id="myDiv" style="position:relative; left:150px;"></div>
    instead of:
    Code:
    <div id="myDiv" style="position=relative; left=150px;"></div>
    well spotted

    I'll edit it.

    Unfortunately it's correct in my source code, so doesn't solve the problem.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  5. #4
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: style.left with external css

    Bah, found a solution but it's browser dependant.

    For Firefox, Opera etc it looks like I need to use
    [highlight="javascript"]
    var my_div = document.getElementById('myDiv');
    var my_style = document.defaultView.getComputedStyle(my_div,"");
    alert(my_style.left);
    [/highlight]

    but for IE I have to use
    [highlight="javascript"]
    var my_div=document.getElementById("myDiv");
    alert(my_div.currentStyle.width);
    [/highlight]

    any alternatives?
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  6. #5
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: style.left with external css

    oh i see the problem now, i had a problem like it before i ended up using inline style tags for the elements i need to access it's style.
    but if you have to theres a way to get CSS attriutes set in an external CSS class by using this:
    Code:
    alert(getComputedStyle(document.getElementById('myDiv'), '').getPropertyValue('left'));
    i tried it in firefox and opera but im not sure if its gonna work in IE

    EDIT: i googled the function above around , it doesnt workin IE but you can use this for IE only
    Code:
    alert(document.getElementById('myDiv').currentStyle['left']);
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  7. #6
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: style.left with external css

    Cheers, that's the same solution I came up with. Guess I'm going to have to make a judgement call as to whether the browser dependant code or the inline styles are going to be more work. I suppose the work put in initially with the browser dependant code will make the site easier to maintain.

    Oh well, it's bed time now. I'll worry about it in the morning.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  8. #7
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: style.left with external css

    hehe glad you figured it out
    good night
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. left join and where
    By Vaielab in forum Database & Database Programming
    Replies: 1
    Last Post: 08-31-2011, 07:12 AM
  2. From left to right rotating
    By Alhazred in forum Website Design
    Replies: 0
    Last Post: 10-08-2010, 09:17 AM
  3. AS: External Style Sheets
    By chili5 in forum Tutorials
    Replies: 3
    Last Post: 09-01-2009, 10:07 AM
  4. HD Space Left
    By Prog in forum Linux/Unix General
    Replies: 2
    Last Post: 06-18-2007, 03:21 PM
  5. Sidebars Left/Right
    By xtraze in forum Website Design
    Replies: 5
    Last Post: 01-13-2007, 03:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

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