How do I load a page using ajax, query, json? [duplicate] - javascript

This question already has answers here:
Loading cross-domain endpoint with AJAX
(9 answers)
Closed 8 years ago.
How can I load a page using jQuery, AJAX, and JSON? I've tried something like this, but it is not working.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<div id="wsww">content.<div>
<script type="text/javascript">
$.get('https://www.google.com/')
.success(function(data) {
$('#wsww').html(data);
});
</script>

You don't need the .success function, instead use a callback function... try this
$.get("<url>", function(data){
$('#www').html(data);
});

Cross Domain Access
HTML
<div id="Content">
</div>
Jquery
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent('http://google.com') + '&callback=?',
function(data){
$("#Content").html(data.contents);
});
Reference
Whateverorigin.org/

Related

How to NOT execute javascript code on specific page [duplicate]

This question already has answers here:
Conditionally load JavaScript file
(8 answers)
Closed 4 years ago.
I have specific javascript code
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//mydomaincom.com/generic/uni.php?g=5"></script>
that work on all pages, but on /ucp.php?mode=register I dont want to execute the code. How to do this?
I made this code but not working (two script inside?)
<script>
if ( window.location.pathname == '/ucp.php?mode=register'{
} else {
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//sk.search.etargetnet.com/generic/uni.php?g=5"></script>
}
</script>
The answer should be with PHP too, if with PHP i can make it.
Try this.
if ( window.location.pathname != '/ucp.php'){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5")
fileref.setAttribute("async","true")
fileref.setAttribute("data-ad-type","iframe v2.0")
fileref.setAttribute("charset","utf-8")
}

Ajax load page + JQuery click doesn't work [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 5 years ago.
Why does my ajax not work?
These are my pages.
I'm using JQuery to check click event and using ajax to load the responder page.
What is wrong in this code?
index.php
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#clicket').click(function() {
$.ajax({
url: 'res.php?rand=' + Math.random(),
type: 'GET'
success: function(results) {
alert(results);
}
});
});
});
</script>
</head>
<body>
<button id="clicket">Hi</button>
</body>
</html>
Responder page:
res.php:
<?php
echo "Worked!";
?>
UPDATE:
I changed my javascript code above.
Put your logic in a document ready. Since your script is in your head, the markup in the body hasn't been loaded into the DOM yet. In order to make your script wait until the DOM has been built, put the logic in a document ready to delay it until that time.
$(document).ready(function() {
$('#clicket').click(function() {
$.ajax({
url: 'res.php?rand=' + Math.random(),
type: 'GET',
success: function(results) {
alert(results);
}
});
});
});
Try using full URL for your ajax call. If you are on live server then write your full domain name, like www.your_domain.com/res.php?rand=.

JSP : using java variable in Javascript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 4 years ago.
I have a JSP page called index.jsp. I have a Java variable called totalCount inside this page :
<%= int totalCount = getTotalCount();%>
Now I want to use this variable in Javascript section to generate a chart:
<script type="text/javascript">
</script>
How can I pass this Java variable inside ? Thanks.
Just assign the value to your Javascript variable.
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
But using scriptlets been highly discouraged and shift to JSTL as soon as possible.
Read : How to avoid Java code in JSP files?
As others pointed out, do not create unnecessary hidden elements on DOM. If you really want to use this variable across files declare this variable on top. Even before script includes, so that it avail across all the files.
Example:
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
<script type='text/javascript' src='js/blah.js'></script>
<script type='text/javascript' src='js/blahblah.js'></script>
Just create a hidden field on your HTML page and access that value using JavaScript.
HTML
<input id='hdn-total-count' type='hidden' value='<%=totalCount%>' />
JavaScript
var totalCount = document.getElementById('hdn-total-count').value;
jQuery (Cross browser compatible)
var totalCount = $('#hdn-total-count').val();
All of the other answers are going to work on inline and on-page JavaScript. But they are not going to work for JavaScript in external files (which are cacheable).
simply you can use
<script type="text/javascript">
var xyz=<%= getTotalCount();%>
</script>
But I dont recommend you to use java codes inside JSP.Please look for JSTL
you can write a code something like this. But it's not a clean way of doing it. Why do you want to do it? You can do it via AJAX calls.
Here is the sample code
<sciprt type="text/javascript">
var myVariable = <%=request.getAttribute("rep");%>
</script>

How do I load a script dynamically via Backbone? [duplicate]

This question already has answers here:
JQuery to load Javascript file dynamically
(2 answers)
Dynamically load a JavaScript file
(30 answers)
Closed 8 years ago.
Inside one of my views, I'd like to load this script:
<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>
Is it possible to do that?
Manually appending it:
$('head').append('<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>')
Using $.getScript:
$.getScript('https://domain.com').done(function () {
// loaded!
});
RequireJS:
require.config({
paths: {
"myscript": "https://domain.com"
}
});
require(['myscript'], function () {
// loaded!
});

Access a function in js [duplicate]

This question already has answers here:
Can we call the function written in one JavaScript in another JS file?
(10 answers)
Closed 8 years ago.
I have a function in my separate js file called hello().
On my page I have this js:
<script type='text/javascript'>//do hello fnc</script>
How can I get the on page script to call the function in my main js file?
Call it in the src attribute of another script tag, before referring to any of its functions in this script.
<script type='text/javascript' src='main.js'></script>
<script type='text/javascript'>
hello();
// Anything defined in previously included js files like main.js
// is now available, as well as anything defined on this page.
</script>
Load the external file first and call the function:
<script src=external.js></script>
<script>
hello();
</script>
You just need to include the external js file:
<script type="text/javascript" src="MyOtherFile.js"></script>
And then wherever in you code you want to call the funciton:
<script type="text/javascript">hello();</script>

Categories

Resources