google.load search <script> - javascript

What does the following do
google.load("search", "1");
Can i access this directly via a <script/> tag.

This is part of the Google Loader service, which allows a JavaScript script to load other common JS script (in this case, the Google AJAX Search API I believe).

Related

How to integrate an external javascript file into PugJS?

I'm trying to make a web application using Node.js and Express and PugJS as a template engine.
My problem: I'm using a google maps API and I want to code all my google maps API functions into a separate javascript file. I can't figure out how to render this external javascript file in the pugJS page and call to functions from the external js file.
Thats pretty easy; you should use the tag script, like follows;
// jquery as an example, include your own script
script(src='/assets/plugins/jquery/jquery.min.js')
script.
// call your functions here
$(function () {
alert('hey')
})

Load JavaScript file source code as plain text and access its content using JavaScript

Is it possible to download a script file using the script tag like this
<script src="http://webserver.com/script.js"></script>
and then be able to read the loaded file content with JavaScript, like this for example:
var scriptContent = window.scripts[0].content;
What i came to notice is that the loaded JavaScript file contents is not accessible by other JavaScript files or am i wrong ?
No, it is not possible to do that.
The source code of a script loaded via src is not exposed via any API made available to JavaScript running in the page.
You could read the value of src and then fetch it using XMLHttpRequest.
The source code of specific functions may be available by calling toString() on them.

Include google api version in a javascript file

I am developing an html/javascript application and want to build a single file containing both HTML and javascript code.
Is there a way to include google-map api in the same file without using a script tag ?
Thanks,
Benoit
Yes - it's called loading asynchronously: https://developers.google.com/maps/documentation/javascript/tutorial#asynch
We need to add script tag to access google map.
There's also a Google Static Maps API that simply creates imagery. It's non-interactive but works without JavaScript:
https://developers.google.com/maps/documentation/staticmaps/

how to load xml file to jsp/html page using javascript/jquery

I have an xml file created in E drive E:\xmlData.xml.
I need to load this xml file into jsp/html file on page load using JavaScript/jQuery.
Generally speaking JavaScript is client-side technology. It doesn't have access to your local file system and you can't load a file from there. But HTML5 provides the File System API. It allows you to solve your issue. Here's an example.
Create a Rest API based server. which will return the xml when the request is given by an Ajax call.
jQuery provides a method $.get which can capture the data from a URL. So to "read" the xml document, it needs to be accessible through a URL.
for using jQuery you must download jQuery javascript library from http://jquery.com/
and put it on the current project/solution file.
use this inside the head tag :
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
This is an example script this will alert xml file content on load...
<script type="text/javascript">
var localFileUrl = '/Yourdirectory/file.xml';
$(document).ready(function() {
$.get(localFileUrl , function(fileContent) {
alert(fileContent);
});
});
</script>

Cross-site javascript problem

I'm developing a JavaScript API service. Main html page looks like this:
<html>
<head>
<script type="text/javascript" src="scripts/logic.js"></script>
<script type="text/javascript" src="scripts/jquery-1..."></script>
<script type="text/javascript" src="http://mydomain/api/main.js"></script>
</head>
...
</html>
In the main.js script I load another script from mydomain. I'm doing it by adding script tag ([script.. src="http://mydomain/api/getsomedata.js?callback_id=3434&someparams=..."]). Loaded script imediatly calls API callback function: MyApi.processCallback(...). Everything works ok.
But when I'm trying to load another mydomain script from local file (logic.js), I recive very strange situation: all script global objects are undefined. There is no MyApi object or jQuery $ object which were visible during previous call. So I can't call MyApi callback function.
Perhaps it is because of security restrictions. Anti-XSS, or something similar. I tried to add a X-XSS-Protection header, like in all Google JavaScript APIs. But it did not help.
I don't use IFRAMES.
The problem can be solved exactly, since many cross-site JavaScript API's are working (Google Maps API, etc.) on the same idea.
What is happening in your script?
If you are doing Ajax calls back to the domain, your script will fail with a Same Origin security error. If you need to do communication with your server from another domain, than you need to use JSONP to do it. If you are working with only modern day browser you can get away with CORS.

Categories

Resources