#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171

    Is it valid Javascript to use window.location as link?


    Hi;

    Is this valid? What't the down side?
    Code:
    onclick="window.location='<?php echo base_url('details/show/'.$row->DID);?>';"
    Thanks
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    Originally Posted by English Breakfast Tea
    Hi;

    Is this valid? What't the down side?
    Code:
    onclick="window.location='<?php echo base_url('details/show/'.$row->DID);?>';"
    Thanks
    Look at your developer's console to see if you receive errors, for code like this. This is one way to validate code, that you question; and... you may also want to check out this JavaScript validation tool, as well.

    JSLint - The JavaScript Code Quality Tool

    There are also other validation tools out there; that you may want to check into too. Some are browser based and some are website based; I think there are even some stand-alone pieces of software out there, as well.
    Last edited by web_loone08; January 13th, 2014 at 08:07 PM.
  4. #3
  5. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Assuming the output of your PHP code doesn't cause any syntax errors from bad quoting, then yes, that is valid.

    However, doing such a thing is generally not advised. If all you're trying to do is link somewhere then use a <a> tag like you're supposed to and you won't need Javascript at all.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by kicken
    Assuming the output of your PHP code doesn't cause any syntax errors from bad quoting, then yes, that is valid.

    However, doing such a thing is generally not advised. If all you're trying to do is link somewhere then use a <a> tag like you're supposed to and you won't need Javascript at all.
    Hey man,
    I know,,,, the whole div is link etc etc...
  8. #5
  9. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    If I was going to do, what your wanting to do; I would use Unobtrusive JavaScript / Degradable JavaScript. That way, if someone has JS disabled in their browser; your link can still be accessible and I would not use inline JS either (that is a personal preference on my part and most developers... using external JS files are easier to update; if you ever need to update anything). So, you may want to do something like:
    Code:
    <script>
    function goTo(where)
    {
    window.location = where;
     this.onclick = function(){
      return false;
     }
    }
    </script>
    
    <a href="http://www.somepage.com/page1.php" onclick="goTo(this.href)"><div>Page One</div></a>
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by web_loone08
    If I was going to do, what your wanting to do; I would use Unobtrusive JavaScript / Degradable JavaScript. That way, if someone has JS disabled in their browser; your link can still be accessible and I would not use inline JS either (that is a personal preference on my part and most developers... using external JS files are easier to update; if you ever need to update anything). So, you may want to do something like:
    Code:
    <script>
    function goTo(where)
    {
    window.location = where;
     this.onclick = function(){
      return false;
     }
    }
    </script>
    
    <a href="http://www.somepage.com/page1.php" onclick="goTo(this.href)"><div>Page One</div></a>
    I don't think it is valid to have <div> inside <a>.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    Why would you need to use a div for that? Anyways according to the html 5 specifications it is now acceptable to have a block-level element inside of an inline element

    Comments on this post

    • web_loone08 agrees
  14. #8
  15. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    Originally Posted by English Breakfast Tea
    I don't think it is valid to have <div> inside <a>.
    Is that not what you just told kicken?
    Originally Posted by English Breakfast
    Hey man,
    I know,,,, the whole div is link etc etc...
    And..., I don't think it is valid either; I was just giving you an example of what I thought you wanted.

IMN logo majestic logo threadwatch logo seochat tools logo