10

I have a page that has multiple divs. I want to get some information from my database to display in some of those divs and I also want it to be displayed as I click on a link to the home div.

I also need the page to be refreshed or reopened in the same window (not in a new page or tab). Last of all, I need the page to be in the home div.

I tried the code below and it didn't work:

<a href="#home" onclick="window.open('index.jsp#home')" >home</a>

<a href="#home" onclick="location.reload()">home</a>

<a href="#home" onclick="location.href='index.jsp'">home</a>
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
MohammadZoghi
  • 578
  • 1
  • 7
  • 17

4 Answers4

15

I used this and it worked

<a href="#" class="home_btn" onclick="location.reload();location.href='index.jsp'">منوی اصلی</a> 
MohammadZoghi
  • 578
  • 1
  • 7
  • 17
12

change your :

onclick="window.open('index.jsp#home')" >home</a>

to

onclick="parent.location='index.jsp#home'">home</a>

no need to reload.

Rikudo Pain
  • 441
  • 9
  • 22
0

like this?

<input id="but1" type="button" value="click"></div>

function loadIndex() {
    window.location.href = "http://jsfiddle.net/Xotic750/u5nmt/";
}

document.getElementById("but1").addEventListener("click", loadIndex, false);

on jsfiddle

remember jsfiddle is in frames

Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

The problem with changing location.href to the current URL with a hash value is that the page won't reload but jump to the given ID.

If you really want to jump to the home div then you can just jump with location.href='index.jsp' and edit your index file to set location.href = '#home' on load.

If you want to be able to pass information across to the newly loaded page (to provide a specific id) you could use the query string instead, e.g. to jump page use location.href = 'index.jsp?loaddiv=foo

Then once the page loads read the query string and use the value to jump to the requested div, e.g. location.search = '#foo'

For details on extracting values from the query string see this question.

Community
  • 1
  • 1
duckman
  • 46
  • 4