-1

In HTML you can write href="#" to prevent a page reload, however in php this doesn't appear to work. Is there an alternative?

It adds # to the existing url, but that's not what I want. I also don't want to remove the href since it replaces the cursor with a select text cursor, and I don't really want to be changing my css for what should be basic php.

I'm sure im just doing something wrong anyway. Thanks!

Josh Lyness
  • 102
  • 1
  • 12
  • ok i didnt realise this, my html seems to be adding # to the url with href="#" too. What can you do to stop it from this? or is this normal behaviour? – Josh Lyness Oct 15 '17 at 18:50
  • write href="javascript:;" instead of # – Subhash Shipu Oct 15 '17 at 18:50
  • `replaces the cursor with a select text cursor` can be fixed with a CSS rule. https://developer.mozilla.org/en-US/docs/Web/CSS/cursor – chris85 Oct 15 '17 at 18:51
  • @SubhashShipu is this really the best way of doing this? it works, but it's not clean. – Josh Lyness Oct 15 '17 at 18:55
  • @chris85 yeah I know cursor: pointer works, but i already said i don't want any css to get involved with this. The css is common across every page, so i'd have to do it inline. empty href's are also invalid html for elements. – Josh Lyness Oct 15 '17 at 18:56
  • @AbdullaNilam I want to prevent the links in my header from working on the page that is currently open. If I leave the home link href="home.html" it reloads the page, and href="#" changes the url. Also causes the page to jump to the top. – Josh Lyness Oct 15 '17 at 18:58
  • @JoshLyness check my answer below – Abdulla Nilam Oct 15 '17 at 19:00
  • @JoshLyness `href` is not required. – chris85 Oct 15 '17 at 19:00

2 Answers2

1

By using a hash you're attempting to tell the browser to navigate to an anchor on the page. If you want to cancel the default behavior and not modify your CSS simply void the anchor's behavior with Javascript:

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

There's a very good description of what this does and why you would use it here: What does "javascript:void(0)" mean?

Robert Wade
  • 4,918
  • 1
  • 16
  • 35
0

Not be the best. But if I had this issue, I'll use onclick

<a href="#" onclick="return false"> texts</a>

Online Preview

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85