HREF=“” automatically adds to current page URL (in PHP). Can't figure it out

前端 未结 8 844
借酒劲吻你
借酒劲吻你 2020-12-01 11:48

Longtime reader of stackoverflow but first question.

I\'m working with Wordpress (specifically thesis theme) in the custom_functions.php file and am finding for some

相关标签:
8条回答
  • 2020-12-01 12:26

    You do realize this is the default behavior, right? if you add /something the results would be different.

    you can do a number of things to prevent default behavior.

    href="#":

    Will do nothing but anchor - not the best solution since it may jump to page top.

    <a href="#">
    

    href="javascript:void(0);"

    Will do nothing at all and is perfectly legit.

    <a href="javascript:void(0);"></a>
    

    href="your-actual-intended-link" (Best)

    obviously the best.

    <a href="<your-actual-intended-link>"></a>
    

    If you don't want an a tag to go somewhere, why use an a tag at all?

    0 讨论(0)
  • 2020-12-01 12:27

    You can just put // in front of $yourUrl in href:

    <a href="//<?=$yourUrl?>"></a>
    
    0 讨论(0)
  • 2020-12-01 12:29

    Add http:// in front of url

    Incorrect

    <a href="www.example.com">www.example.com</span></p>
    

    Correct

    <a href="http://www.example.com">www.example.com</span></p>
    
    0 讨论(0)
  • 2020-12-01 12:31

    Use this:

    <a href="<?php echo(($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">Whatever</a>
    

    It will create a HREF using the current URL...

    0 讨论(0)
  • 2020-12-01 12:36

    A solution that works no matter if you are developing on a local server or live is to add "//" in front of your link. This will effectivly remove the URL of the site you are currently on.

    Example:

    <a href="www.google.com">Google.com</a>
    

    This will in output http://localhost/mySite/currentPage/www.google.com

    What you should do instead is this:

    <a href="//www.google.com">Google.com</a>
    

    This will output www.google.com

    0 讨论(0)
  • 2020-12-01 12:43

    In any case, your code will generate invalid markup: You shouldn't wrap block contents in a link. tags don't work like this. If you want this effect you should use js or create an absolutely positioned link above the content (z-index). More on this here: Make a div into a link.

    You should make sure to validate your code when it renders: http://validator.w3.org

    0 讨论(0)
提交回复
热议问题