Anchor HREF, if I click again the anchor the link on the address doubles or it concatenate on the existing link. What's the best way to avoid this?

China☆狼群 提交于 2019-12-12 01:40:38

问题


Good Day! I've got some trouble on PHP. If I click an href which is this <a href = 'members/loans/loan-application-form.php'> it goes to the loan-applicaton-form.php, but if i click it again inside the loan-application-form.php (cuz it is located at the navbar that exist in all pages) the href of the anchor concatenates on the existing url and it creates an error telling that address is not existing. I already knew that the problem is it searches the address on the current directory and probably it will return an error because there is no existing address on the directory because it is the origin or the parent. What is the best way to avoid this?


回答1:


Add a base tag to your header. This will prepend all HTML URL's with the given base URL. (Including images, CSS and JS calls)

<head>
    <base href="http://www.yourdefaulturl.com/">
</head>

Your URL will then be http://www.yourdefaulturl.com/members/loans/loan-application-form.php




回答2:


You can use an absolute path

<a  href = 'http//your.site.cpm/members/loans/loan-application-form.php'>

Advice : you can read this article

or this stack question

Or you can use ../ to navigate inside your relative path

Lets imagine your nav bar is located at /navigation/navbar.html

Then you can have a relative path like

<a  href = '../members/loans/loan-application-form.php'>



回答3:


By specifying relative Urls like so:

<a  href = 'members/loans/loan-application-form.php'>

You are simply stating you wish to go form the current page to this page, hence why it is being added to the end of the current URL. The best way to avoid this quite simply is to set an absolute URL like so:

<a  href = 'http://projectname/members/loans/loan-application-form.php'>

Even when not using http:// it is often still considered relative so be sure to include this.

Another way to do the same but slightly quicker would be to add a variable in say your header file for example:

$url = 'http://example.com';

Then when specifying the URLs you can do say:

<a  href = '<?php echo $url;?>/members/loans/loan-application-form.php'>

This just means that should you say change your domain, rather than editing every URL you can simply edit the variable value and be done with it.



来源:https://stackoverflow.com/questions/31241526/anchor-href-if-i-click-again-the-anchor-the-link-on-the-address-doubles-or-it-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!