Page Reload while clicking on the href link - javascript

I have a web application in which index.jsp is a welcome page. Index.jsp contains the UI code . And i have multiple html files which has the same href link . My problem is whenever I click on the link the UI is reloaded. So when the url is same, the UI should be maintained and the page should not be reloaded.
I am new to web application development . Please help me on this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="myform_sample1">
<a href="http://localhost:8080/app/index.jsp?file=filepath" target="http://localhost:8080/app/index.jsp?file=filepath">sample1<br>
</a>
</div>
<div id="myform_sample2">
<a href="http://localhost:8080/app/index.jsp?file=filepath" target="http://localhost:8080/app/index.jsp?file=filepath">sample2<br>
</a>
</div>
</body>
</html>

you can try this:
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
});
on a click event you can transfer values and get back through ajax without reloading ur UI

I think you might be looking for jQuery:load, get, ajax

Related

Jquery TypeError when replacing page content

I'm trying to achieve the following:
A simple Java Spring application, after receiving a GET request, sends me an HTML page with a form. When the button is pressed in this form, there is a POST request with an XML content sent back to the Spring app, which, in turn, responds with an HTML page. I would like to be able to display this response as a proper page in a browser, including all the scripts being downloaded and fully functional.
The form HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<meta charset="UTF-8" />
<title>Request Report Form</title>
</head>
<body>
<form method="post" action="">
<script>
function sendXml()
{
event.preventDefault();
const request = '<?xml version="1.0" encoding="UTF-8"?>\n' +
<!-- skip -->
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: request,
contentType: 'application/xml;'
}).done(function (data) {
$("html").html(data);
}).fail(function (data) {
$("html").html(data.responseText);
});
}
</script>
</form>
</body>
</html>
The $("html").html(data); part above is supposed to replace the current page's content with the content received from the server, as far as I understand. But the page is not being displayed. There is an error message in the browser's console: "TypeError: n.head is null", it seems to have to do with JQuery, see the image link.
My questions are, why does JQuery kicks in and what is it trying to do? And how do I fix this error?
I don't know much about frontend side of development, so please go easy on me ;-)
Edit
Just realised that my first question is moot, I am trying to display the response page using JQuery, so obviously, it kicks in at that point. But the call fails for some reason.
The content of the response from the server that I'd like to see rendered by the browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Progress Report</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body data-user="eyJjaGFydCI6eyJyZW5kZXJUbyI6IlByb2dyZXNzIiwidHlwZSI6ImNvbHVtbiJ9LCJzZXJpZXMiOlt7Im5hbWUiOiJQcm9ncmVzcyIsImRhdGEiOlsxLDEsMCwwLDAsMCwwLDAsMCwwXX1dLCJ0aXRsZSI6eyJ0ZXh0IjoiUHJvZ3Jlc3MifSwieEF4aXMiOlt7InR5cGUiOiJsaW5lYXIiLCJjYXRlZ29yaWVzIjpbIlJlZ2lzdGVyZWQ8YnI+KDQuNi4xMykiLCJTZXNzaW9uIDE8YnI+KDEyLjcuMTUpIiwiU2Vzc2lvbiAyIiwiU2Vzc2lvbiAzIiwiU2Vzc2lvbiA0IiwiU2Vzc2lvbiA1IiwiU2Vzc2lvbiA2IiwiU2Vzc2lvbiA3IiwiU2Vzc2lvbiA4IiwiRmluaXNoZWQiXX1dLCJ5QXhpcyI6W3sidGl0bGUiOnsidGV4dCI6IlNlc3Npb24gU3RhcnRlZCJ9LCJtaW4iOjB9XX0=">
<div id="wrap">
<div class="container">
<div class="report"></div>
</div>
</div>
<script>
window.onload = function (event) {
event.preventDefault();
$(".report").append("<div class='chart'><div id='Progress'><img src='staticMedia/img/loader.gif' /></div></div>");
userData = $("body").data("user"); // get user report data
var chart = new Highcharts.Chart(JSON.parse(atob(userData)));
// destroys legend if only 1 series
if (chart.series.length < 2) {
chart.legend.destroy();
}
}
</script>
</body>
</html>
Found an answer here: stackoverflow.com/a/11984907/3182810. Replacing
$("html").html(data);
in the form with
document.open();
document.write(data);
document.close();
does the trick.

How to pass Thymeleaf object from base page to ajax generated page?

I have a registration process that has substeps and I have individual HTML pages for these substeps for easier management. Now I have a "base page" which is the container. The following div is where I load the subpages.
<div id="substeps" th:object="${stepFourForm}">
</div>
and I load the substeps using ajax
$('#substeps').html(data);
How can the loaded substep page retrieve the stepFourForm object on the base page?
You should remove div content before loading substep page. And you can do it using thymeleaf fragments.
$.ajax({
type: "GET",
url: "/example-url",
success: function (data) {
$('#substeps').html('');
$('#substeps').html(data);
}
});
Controller code:
#RequestMapping("/example-url")
public ModelAndView example() {
ModelAndView view = new ModelAndView("substep1 :: content");
return view;
}
Example fragment html name substep1:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:dt="http://www.thymeleaf.org/dandelion/datatables">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<div th:fragment="content">
--some content
</div>
</body>
</html>

Javascript: like Facebook posts using facebook javascript sdk?

I'm fairly new in using Facebook javascript SDK and I have no idea what I am doing!
I'm trying to like a facebook post using Facebook javascript SDk in my html page.
My full code this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="bod" style="width:100%; height:100px; background:#000;"></div>
<script>
$( "#bod" ).click(function() {
FB.api("/10153XXXXXXXXXX/likes", 'post',function(response) {
if(response === true) {
alert("done!");
}
});
});
</script>
</body>
</html>
I thought this code will like the given post (its ID is 10153XXXXXXXXXX) once i click on the div #bod but when I click on the div, nothing happens and I don't even get the alert(); at all!
could someone please advise on this issue and let me know what I'm doing wrong?
First of all you'll need to initialize the FB SDK.
Then, in order to call the API you'll need a valid access token therefore, you'll need to login the user using your App.
I would suggest to double check the docs again:
https://developers.facebook.com/docs/javascript
https://developers.facebook.com/docs/javascript/examples
In the second link you'll find an example to login a user and then call the API that I believe will be helpful for you.
I hope it helps.

JQuery .load() does not work

I want to load the content of a webpage in a div. Here's my code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
When the this item is clicked, the webpage should be loaded inside the content id:
<ul><li><a id="menu_top" href='testing.html'><span>My account</span></a></li></ul>
<div id="content"> </div>
JS:
$("#menu_top").click(function() {
var href = $ (this).attr('href');
alert(href);
$("#content").load(href);
return false;
});
testing.html:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
SUCCESS!
</body>
</html>
Unfortunately, the above code does not work. I have checked it multiple times but could not find the issue.
#Milind Anantwar is correct - must have the $(document).ready wrap your JS:
$(document).ready(function(){
$("#menu_top").click(function() {
var href = $ (this).attr('href');
alert(href);
$("#content").load(href);
return false;
});
});
I also tested whether or not the UL or OL tags affected it as #psicopoo mentioned, but it didn't seem to in Chrome. *ALSO, make sure that "testing.html" is a valid page to load.
*As noted on http://api.jquery.com/load/: "Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol."
I think you're encountering an issue with the page you're trying to load. I copied your code exactly and it works in Fiddle, see if this works for you, if so, it's probably your page:
testing.html
JS Fiddle:
http://jsfiddle.net/fb4j6y6d/

Highslide open html onload

I am a newbie so treat me gently.
I want to open an Highslide html window on load. I have seen an explanation on the old "Highsoft" site but cannot make it work.
Here is my test file without any script to make it open onload:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Open on load - test</title>
<script type="text/javascript" src="highslide/highslide-with-html.js"></script>
<script type="text/javascript" src="highslide/highslide.config.js" charset="utf- 8"></script>
<link rel="stylesheet" type="text/css" href="highslide/highslide.css" />
</head>
<body>
<div>
<a href="#" onclick="return hs.htmlExpand(this, {
width: 400, creditsPosition: 'bottom left',
headingText: 'Stoke Gabriel Boating Association', wrapperClassName: 'titlebar' } )">Inline HTML</a>
<div class="highslide-maincontent">
<h3>Next Sailing Event</h3>
The next sailing event will take place on June 23.
</div>
</div>
</body>
</html>
What do I need to do to make the Highslide window open onload as well as keeping the clickable link?
Best Wishes
Geoffrey
First, you need to use the full Highslide script, not the stripped-down highslide-with-html.js version. Use highslide-full.js or highslide-full.min.js (a compressed version of the full script).
Next, your href needs a unique ID:
<a href="#" id="image1" onclick="return hs.htmlExpand....
The ID can be anything, as long as it's unique.
Finally, add this to the Highslide config options in your highslide.config.js file:
hs.addEventListener(window, "load", function() {
document.getElementById('image1').onclick();
});
hs.addEventListener(document, "ready", function() {
document.getElementById('image1').focus();
});
A good way would be to make a function fire when the document is ready.
if (document.readyState === "complete") { window.open(URL,name,specs,replace) }
Extra information:
Javascript - How to detect if document has loaded (IE 7/Firefox 3)
http://www.w3schools.com/jsref/prop_doc_readystate.asp
http://www.w3schools.com/jsref/met_win_open.asp

Categories

Resources