How to use Google Code - javascript

I use openlike (openlike.org) but the site seems to be temporarily down. I would normally use something like this:
<script type="text/javascript" src="http://openlike.org/v1/openlike.js"></script>
<script type="text/javascript">OPENLIKE.Widget()</script>
But that isn't working. I've found the project on google code, I was wondering how I implement exactly the same thing, but from Google? Is Google code completely different from the google cdn? I.e. I can't just change the URL of that javascript file?
Google code project: http://code.google.com/p/openlike/source/browse/#svn%2Ftrunk%2Fv1%253Fstate%253Dclosed
Thanks for any help,
Dave

From the link posted, if you click on "openlike.js" and then "View raw file" you'll get exactly that:
http://openlike.googlecode.com/svn/trunk/v1/openlike.js
You can certainly link to this file. Here's what I tried and it worked fine:
<script type="text/javascript" src="http://openlike.googlecode.com/svn/trunk/v1/openlike.js" ></script>
<script type="text/javascript" >
console.log(OPENLIKE);
</script>
I don't know enough about google code and svn to tell you if that file will be there forever and won't change. I doubt that it's intended to be used a CDN. But it's probably good enough to use while openlike.org is down temporarily.

Related

Google Translate javascript snippet not working

I tried using the code snippet from w3school.com. It worked on w3school but doesnt work on my PC.
<div id="google_translate_element"></div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
I got the following in the console.
translate.html:18 GET file://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit net::ERR_FILE_NOT_FOUND
The snippet over at w3school indeed has a bug.
It says to add the following line to include Google's API:
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Unfortunately the trailing // makes it point to a local file. So unless you've downloaded the library and bundled it with your html file this points to nowhere.
Instead link to the online library by adding https:
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
When you're running it on your machine, you're running it as a local file. As such, the source file, which is loading from //translate.google etc, is trying to find this file on google.
If you replace this with:
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
you will find it is no longer trying to find a local file (i.e. on your machine), but instead will look for it on the internet.
pure javascript, can be integrated from browser debug console, or in webview, or any site. In this example we just find element and change his content to button (you can change any element you want)
importScriptURI("https://translate.google.com/translate_a/element.js");
document.getElementsByTagName("h1")[0].innerHTML='<div id="google_translate_element"></div>';
setTimeout(()=>{ new google.translate.TranslateElement({pageLanguage: 'en'},'google_translate_element');},1000);

ARC packaged Android application: run JavaScript API command

I have an ARC packaged ChromeOS application and since there are some behavioral differences between ChromeOS and Android I want it to make some JavaScript API call: chrome.power.requestKeepAwake.
After the obvious step of adding "power" permission, what I've tried to do is to change contents of app_main.html:
<!DOCTYPE html>
<!-- these are the lines I've added -->
<script type="text/javascript">
chrome.power.requestKeepAwake("display");
</script>
<!-- until here -->
<iframe src="_modules/mfaihdlpglflfgpfjcifdjdjcckigekc/main.html"></iframe>
But this lead to no changes.
I'm sure, that the request is not applied as I've tried to run the same query from Chrome console and it did the thing.
How should I manage to embed this code?
Actually, I've managed to solve this issue.
The reason for it to happen is not that the code is applied in the wrong moment or something like this, but that the code you use in the app_main.html can't contain inline JavaScript, which is insecure.
That said, the code should look like:
/app_main.html:
<!DOCTYPE html>
<script type="text/javascript" language="javascript" src="power_request.js">
</script>
<iframe src="_modules/mfaihdlpglflfgpfjcifdjdjcckigekc/main.html"></iframe>
/power_request.js:
chrome.power.requestKeepAwake("display");

linking code from a separate document to work on another page

I have taken script from a webpage document I have made and I have saved onto a notepad document with the extension .js.
I would like to now know how I can reference this .js file from the current page that I have created so that the script will run on that page without the actual code being there, just the reference link.
Something like this?
<script type="text/javascript" src="myfile.js" />
It must be a reference link. There are other techniques besides the standard, but they all rely on linkage. You can't beat the linkage. You can't stop the linkage. You mus succumb to the linkage.
That's not how the web works? You might be able to use some sort of developer tool to execute arbitrary javascript from a file when the page loads, but that would just be overriding the default way the web works.
you will need to do :
<script type="text/javascript" src="URI_TO_DIR/extension.js"></script>
see this page about embedding a javascript file
if script is on your computer, it will of course not be accessible on the web, for that you'd need a webserver or a webspace
You can include an external JavaScript using a script tag with a src attribute. For example, something like
<script type="text/javascript" src="javascriptfile.js"></script> should do what I think you're asking for.

turning on jquery?

I have website A: http://dl.dropbox.com/u/3264697/calc/v2/index.html
It is working as intended, 100%.
I have website B: http://pe2kasb.net63.net/
It is not working as intended. Website B is a file for file mirror of A.
I assume then, this is something wrong with the host. Looking at the Javascript console, the error appears to be related to the host:
screenshot here http://img825.imageshack.us/img825/4782/unlednwe.png
Need I contact them, or is there something I can do...? I'm new to JQuery, and I believe that's what is the root of the issue but... i'm not sure.
You are trying to load jQuery from ../v2/media/js/jquery.js but there is no such file in the second website.
I recommend that instead of hosting JQuery yourself, you use a hosted version:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript">
</script>
This post explains the reasons.
You have invalid jQuery path at http://pe2kasb.net63.net/
Replace
<script type="text/javascript" src="../v2/media/js/jquery.js"></script>
with
<script type="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
Also you have dataTable.js missing.
You are loading JQuery from ../v2/media/js/jquery.js which does not seem to exist...
To solve the issue and improve the speed of the website
do not store JQuery on the server, rather load it from a 3rd party (Google works well)
use JQuery minified version
To load JQuery from Google you can just use:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
More information here
EDIT: note that the above automatically loads the minified version of JQuery. If, for whatever reason (?) you wanted to load the uncompressed version you could use
google.load("jquery", "1", {uncompressed:true});
Look what http://pe2kasb.net63.net/v2/media/js/jquery.js refers to.
The file does not exist.
Upload it or include it from another location.
Make sure that jQuery is correctly linked, currently jQuery is supposed to be at "../v2/media/js/jquery.js" on "http://pe2kasb.net63.net/", which means go back one folder then enter "/v2/media/js/jquery.js".

jquery: how do i know if i have it?

i need jquery to work on a browser locally. how do i know if it is installed and how do i install it ?
my question is specific to being able to run this code:
onmouseover="evt.target.setAttribute('opacity', '0.5'); $('#someDiv').show();"
onmouseout="evt.target.setAttribute('opacity', '1'); $('#someDiv').hide();"
You can test if jQuery is loaded by opening your javascript console (in Chrome: Settings > More tools > Javascript console).
Where the little blue arrow appears type:
if(jQuery) alert('jQuery is loaded');
Press enter.
jQuery isn't installed like a program, it's a file that needs to be included in your source code somehow, the most common practice is to include by adding <script type='text/javascript' language='javascript' src='local/path/to/jquery.js'></script> in the <head> section of your page.
If you are going to include jQuery locally, as per Robert's suggestion, you will first have to download it from here: http://code.jquery.com/jquery-1.4.2.min.js
put this right above your closing body tag:
<script src="//ajax.googleapis.com/ajax/libs/jquery/[jquery version here]/jquery.min.js"
language="javascript" type="text/javascript"></script>
jquery isn't "installed" into a browser. it's a js library referenced from the web page you are viewing.
EDIT: This works if you have internet access. If not, you will have to download that file to your local system and reference the local path.

Categories