Santa Dan Dumitru
Santa Dan Dumitru

Reputation: 15

link tag calling js function don't work

I have a static site with classic menu like: home, services, contact etc links. What I'm trying to do is reducing the site's size. So, instead of using multiple pages for links (like home.html, services.html) I'm trying to keep only one page and then (when clicking occurs) dynamically change content.

html code (sample):

<a href="javascript:this.location.reload()" onClick="ContactClick();">contact</a>
<h1>old text</h1>

js code:

function ContactClick() {
    $('h1').html('new text to change, but it doesnt work, yay!');   
}

Something funny happens. If I click on the link, 'old text' becomes indeed 'new text', BUT ONLY FOR 1 SECOND! And then, it changes right back to 'old text'!

I have tryied calling like this: <a href="javascript:ContactClick();"> and it works just fine, but still I need to refresh the page when click event occurs (I have some color changing stuff on site, lol).

  1. Is there another way to reduce site's size, or my idea is just fine? OR
  2. Is there a solve including refreshing the site and calling the js junction from link?

Upvotes: 0

Views: 203

Answers (2)

PauliL
PauliL

Reputation: 1269

You do not need to reduce the site's size. Static pages that contain only text are small and load in a fraction of a second even at the first time. After that, they will be in cache and load instantly.

On the other hand, if you put all the text in one page, plus add some javascript code, the result is not smaller but bigger than the static pages.

Further, you should remember that javascript does not always work. For example, many people disable javascript for security reasons or to avoid irritating advertisements. Therefore, navigation should never be dependent on javascript. At the very least, you should have noscript part that works with static pages in case javascript is disabled. (But this will make the page even bigger.)

Upvotes: 1

lanzz
lanzz

Reputation: 43198

Perhaps you shouldn't be reloading the page in your href.

Upvotes: 5

Related Questions