Changing index.jsp dynamically without reloading the page

2

In my index.jsp I have a table that in one of its cells and an iframe that displays one of my jsp, and depending on the type of action taken by the user it changes which and the page displayed inside the iframe, I would like know if you can do this change without reloading the page.

So I researched how to do this and in ajax, but I do not know how to use ajax.

    
asked by anonymous 19.11.2014 / 14:45

2 answers

2

There are at least three main ways to change the contents of a frame or iframe .

Change the src attribute with JavaScript

Imagine you have a frame like this:

<iframe id="frame" src="http://servidor/pagina1"/>

Thenyoucanimplementafunctionthataccessestheframethroughitsidandchangesthesrcattribute,whichcontainsthedisplayedURL:

function trocaPagina() { var f = document.getElementById('frame'); f.src = 'http://servidor/pagina2'; }

This can be called through some link or button, for example:

<button type="button" onclick="trocaPagina()">Trocar conteúdo</button>

Demo on JsFiddle

Through a link ( a ) with the attribute target

Create a frame with the name attribute set as follows:

<iframe name="frame" src="http://doc.jsfiddle.net/"/>

Thenyoucanopenapageonitwhentheuserclicksonalink,justsettheURLonthelinkandaddthetargetattributewiththesamevalueasthe%frame%.

Example:

<a href="http://blog.jsfiddle.net/" target="frame">Trocar outro conteúdo</a>

Demo on JsFiddle

Through a form submit with the name attribute%

Considering the same previous frame with the attribute target :

<iframe name="frame" src="http://doc.jsfiddle.net/"/>

Soyoucanopenapageonitwhentheusersubmitsaform,justsettheURLinthetributenameandtheactionattributewiththesamevalueasthetargetoftheframe.

Example:

<form action="http://jsfiddle.net/" target="frame"> <button type="submit" onclick="trocaPagina()">Trocar conteúdo</button> </form>

Demo on JsFiddle

    
19.11.2014 / 15:13
2

You can use JavaScript to change the iframe address without having to reload the entire page:

document.getElementById("id-do-iframe").src = "/nova-pagina.jsp?parametro=1";
    
19.11.2014 / 15:03