I want to retrieve source code of a javascript file( eg: xxx.js) from some url. I could get html from url but when I try to do get .js file using same method, it fails.
How can I get a whole source of js file from url?
Here's what worked for HTML :
$.ajax({
url : "http://localhost:8080/aaa/bbb/xxx.html",
success : function(result){
var a= result;
alert(a);
}
});
Use this
<script type="text/javascript" src="xxx.js"></script>
Related
I have a separate js file into laravel public folder, I have decleard my route into web.php, I can use them in blade file but getting error while try to use them in that js file.
$.ajax({
//url:"http://127.0.0.1:8000/cats/fetch",
url:"{{route('cats.fetch')}}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#'+dependent).html(result);
}
})
but when I use hard coded url then its work for instance url:"cats/fetch".
How can I make it configurable not hard coded into js file
url:"{{route('cats.fetch')}}", is not valid .js syntax. You're correct that you can use it in .blade.php, but in an external .js file, you need to assign it to a variable first:
<script type="text/javascript">
let url = "{{ route('cats.fetch') }}";
</script>
<script src="path/to/file.js" type="text/javascript"/>
Then, in file.js, reference the variable:
...
url: url,
...
Here is a problem with "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; when I using this code in a php file, which I called from a JS (with fetch or XMLHttpRequest), the output will be the the current url of this .php file!
Example I call PHP in JS (on my wordpress site):
const item = document.querySelector('.item');
fetch('path/wp-content/themes/current_theme_directory/called.php')
.then(res => res.text())
.then(responseText => item.innerHTML = responseText);
And my called.php file contain this code:
$current_url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $current_url;
My responseText in my JS this: path/wp-content/themes/current_theme_directory/called.php
In this situation how can I get the real current url, what shows my browser also?
you can get the current url using javascript, it's in a variable called window.location.href
console.log(window.location.href); will log the current url in the console.
Technically $_SERVER['HTTP_REFERER'] will have the url that loaded the PHP, but keep in mind that info is easily edited by a crafty user.
I usually use "getElementById()" to detect internal file data . I tried this to detect data from an external file but its not work :
<script>
var outterPage ="dom1.html";
var temp=outterPage.getElementById("abc").innerHTML;
</script>
what can i do to detect data from external file ?
You can use the jQuery function $.get to read the contents of a URL into a variable. Then you can parse that into a DOM element to find parts of it.
$.get("dom1.html", function(data) {
var temp = $("<div>", { html: data }).find("#abc").html();
// do something with temp
});
You can also specify the ID in the URL argument to $.get, so jQuery will just return the contents of that part of the page.
$.get("dom1.html #abc", function(temp) {
// do something with temp
});
You can use jquery .load() to load the external file into a dom element and then get its value.
<script>
$(document).ready(function(){
$("#holder").load("dom1.html");
var temp=$("#holder").find("#abc").innerHTML;
});
</script>
I am looking for a way to parse a local JSON file from a none-hosted HTML page (e.g.: file:///C:/Folder/To/Your/HTML/default.html) without using AJAX since it will only entail an allow access-origin error.
I also cannot use JSONP since the HTML is not hosted to any hosting server (whether locally or remotely hosted).
suppose your json file 'json.data' is like below:
data = '[{"name" : "Harry", "age" : "32"}]';
you have to load the json file into html head section like below:
<script type="text/javascript" src="file.json"></script>
and then in body section you have to call the javascript function to parse json data like..
<body onload="load();"></body>
javascript code:
<script>
function load() {
var mydata = JSON.parse(data);
alert(mydata[0].name);
alert(mydata[0].age);
}
</script>
that's it.
I'm trying to get the info inside the JSON file that itunes lookup returns, but my code doesn't work.
If I save file that this url returns, which would be a .js file, and upload it to my server and pass in the url of that file it works, but when I'm trying to read file directly from itunes it doesn't.
Please help me out if you have any clues.
Thanks
<script type="text/javascript">
$.getJSON( "http://itunes.apple.com/lookup?id=600172326", function(data) {
var icon = document.getElementById("WallpaperIcon");
var description = document.getElementById("WallpaperDescription");
icon.src = data.results[0].artworkUrl100;
description.innerHTML = data.results[0].description;
});
</script>
iTunes probably doesn't have CORS enabled, so you can't make an Ajax request to the service. However, luckily for you it seems to support JSONP:
$.getJSON('http://itunes.apple.com/lookup?id=600172326&callback=?', ...);
More about $.getJSON and JSONP: http://api.jquery.com/jquery.getjson/#jsonp
DEMO