Uncaught Reference Error: function is not defined - javascript

I'm working on an app for webOS that will take a megaupload link(assuming its a video), parse it into a direct link and stream it to a ffplay port for webos called TouchPlay.
I'm working on a basic prototype that will just take the end code on a megaupload link and return a direct link.
Now that I have laid out the basic parts of my application, on to my question. I want to take data in a form in an html page and feed it into a function in an external js script.
Here's the html portion
<html>
<head>
<script LANGUAGE="JavaScript" src="source/main.js"></script>
</head>
<body>
<form NAME="myform" ACTION="" METHOD="GET">
Enter the MegaUpload Code, ex. megaupload.com/?d=glgrn8f1 -> glgrn8f1: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE="">
<INPUT TYPE="button" NAME="button" Value="Click" onClick="javascript:codeIn(this.form);">
</form>
</body>
</html>
And in my main.js file is in a subdirectory called source and it contains function codeIn(code){ plus the rest of the functions.
Yet when I enter some test data in my web page and hit submit, I get an error in the chrome dev console saying Uncaught Reference Error: codeIn is not defined
I have a very limited experience with html and javascript, but I cant seem to figure this out, I imagine its something really simple that I'm missing.
EDIT: Here's main.js, and yes, I have checked for typos. Just ask if you need to see the contents of any of these functions, but I didn't think they were neccesary.
function codeIn(code){
...
}
function getInfo(url){
...
}
function waiting(timer){
...
}
Ive looked through all these suggestions and it all seems to point to a syntax error, yet when I try to put all the js functions into the head of the html, it works. So thats what I'm doing. I don't change a single thing when I reference it from source/main.js, but whatever.

From the look of it, I think you then need to double check the location of main.js. Verify whether your HTML file is in a location from where main.js can be accessed as source/main.js, rather than something like ../source/main.js (in case your HTML file is in a folder parallel/sibling to the source folder)

The RefereceError in general related to the scope of function that is present and the function call that is being made. It might the source path of the js, html/xhtml/jsp or function scope with in the js file. Hope this helps. Thanks.

Related

How to get a DIV element from an external webpage in HTML file?

Apologies in advance if this question has been asked earlier. I did find some similar questions on web but I couldn't figure out the answer still. You can say I have never dealt with anything beyond basic HTML. So any help would be appreciated.
I have a HTML file (Say text.html) only for personal use. In the file, there will be an input box for entering text and a submit button. I want that if I clicks on submit, it opens a particular hyperlink from an external webpage based on the input text. I guess it's like "I am feeling Lucky" of Google.
Example: If the user enters "Test" and clicks on Submit, it should open the second result from the page "https://www.google.com/search?q=test"
Here is my HTML:
<!DOCTYPE html>
<html>
<body style="background-color:beige">
<h1 style="text-align:center"><font size="14">Test</font></h1>
<style type="text/css">
</style>
<form id="form">
<div align="center" style="vertical-align:bottom">
<input type="text"
value="Test"
id="input"
style="height:50px;width:200px;font-size:14pt;">
</div>
</form>
<TABLE BORDER="0">
<TD><button class="button" id="button01">SUBMIT</button></TD>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button01').click(function(e) {
var inputvalue = $("#input").val();
window.open("https://www.google.com/search?q="+inputvalue);
});
</script>
Also, here is the example of the div element from the page on which the hyperlink I want to open is on:
<div id="XYZ" class="contentEditValue" style="float:left;width:180px;">
2nd Result
</div>
I have read that it can be achieved with PHP or Jquery and all but they are not something I have ever worked on. Thank you very much in advance for any help!
Appreciate any other alternatives as well.
You shouldn't be able to do that because of security. If that (reading content from iframes, other browser windows...) would be possible, an attacker could add JS keylogger to your internet banking login or read your messages on Facebook. CORS (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is used to block these requests and if the website doesn't say explicitly that you are allowed to do something with its content, most browsers won't allow you that.
You have are missing a }); to close the ready() function
<script type="text/javascript">
$(document).ready(function(){
$('#button01').click(function(e) {
var inputvalue = $("#input").val();
window.open("https://www.google.com/search?q="+inputvalue);
});
});
</script>
Here's a basic example of how to do this in PHP.
Taking JavaScript/JQuery out of the picture, let's just say you have a basic form:
<form>
<input type="text" value="Test" name="input">
<input type="submit">
</form>
Without specifying action or method attributes on the <form> tag, the form will make an HTTP GET request to the URL of the page it is on, so for this example the PHP code will be on the same page as the form. Here's a more detailed description of sending form data if you're interested.
Now that you have a way to pass the input to the PHP script*, there are three basic parts to this problem.
Make a request to the page you want with a query string including your input
http_build_query is an easy way to construct a properly encoded query string to use with your request. For this example we'll use file_get_contents to make the request. There are other ways to do it, including cURL, but let's keep it simple.
$query = http_build_query(['q' => $_GET['input']]);
$page = file_get_contents('http://www.example.com/?' . $query);
I'm not using Google for this example because it's a bit more complicated to find the right links in the response and follow them. (Partially because they don't really want you to do it that way.)
Find the link you want in the response
Don't try to find the link in the response with regex. You'll have problems with it, come back to Stack Overflow to try to solve them, and people will tell you that you shouldn't be using regex, so just skip that part and use a DOM parser.
$doc = new DomDocument;
$doc->loadHTML($page);
$links = $doc->getElementsByTagName('a');
$url = $links[0]->getAttribute('href');
I used getElementsByTagName() to find links, but if the page is more complex an xpath query will work better. Also, I used the first link ($links[0]) because example.com only has one link. $links[1] would get you the second link if it existed.
Follow the link
header("Location: $url");
exit;
If everything goes well, you'll end up where you want to be. But there are a lot of things that can go wrong. If you're requesting a resource that you have no control over, it can change at any time without any advance warning to you, so your code that finds the link may stop working. You may get blocked from making requests. Scraping links from sites like this violates the terms of service on many sites, so check that out beforehand. You may find that the site offers a web API, which should be a much better way to access its content than this.
*You don't really need a form for this; you can just pass the input parameter in the URL to your page.

javascript function not defined when hit back button

I have the following example files:
index.html
<body>
<input type="file" name="image" onchange="handleUpload()">
<script>
function handleUpload() {
window.location = 'aa.html';
}
</script>
</body>
aa.html
<body>
success
</body>
After file upload I'm redirected to another page.
When I hit browser back button I get Uncaught ReferenceError: handleUpload is not defined at HTMLInputElement.onchange.
Can someone explain why is this happening? I understand why handleUpload is triggered but why it's undefined?
Update
My app is more complex. I use webpack to bundle my js files into a single file then I reference it in layout before closing body tag.
What i'm trying to do is a image preview when input file is filled. Everything works great but if I hit back button after the form was submitted I get that error.
I manage to avoid this problem by removing onchange from html and added a event listener in one of js file. This approach has a delay on opening the select file window.
Here is your Answer
<input type="file" name="image" onchange="window.location='aa.html'">

Retrieve variable name from an HTML site

I am trying to retrieve simple javascript variable (which is written to a File Systems Object) from a website which is served by an apache host on my ubuntu laptop.
So I have the function that writes the variable set up as follows:
<script type ="text/javascript">
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("/home/lex/Downloads/goal.txt", true);
s.writeline(document.passForm);
s.Close();
}
</script>
and the section that takes the user input from the html website is
<div id="bot-right">
<form onsubmit="WriteToFile(this['goal'].value)">
<a align = "left"> <b><Strong>Enter a Goal name</Strong></b></a><br>
<input type="text" name="goal"> <br>
<input type="submit" value="Send Zeus">
<br>
</form>
</div>
For some reason, when I type in variable names to the form on the website, the file goal.txt gets created in the directory, /home/lex/Downloads/, but nothing gets written to it.
I also noticed that when I delete the goal.txt file and rewrite the variable from the html website, the file doesn't always get created.
I am not a JavaScript person and I am at a loss as to what I may need to fix this.
My intention is to get the variable written to the text file and have a processing c++ file process the variable.
Would someone be kind enough to lend an insight?
Thanks!
one way to do it is just calling the function without parameters and just getting the input value like this:
adding and id or a class to your input to get that specific value:
document.getElementById('goal').value
document.getElementByClass('goal').value
Or getting the value by name:
document.querySelector('[name="goal"]').value;
EDIT1
You could add a console.log to check if the value is beign passed correctly like this:
var inputValue = document.querySelector('[name="goal"]').value;
console.log(inputValue);
And if the value is being passed then the problem is your writeline or in the creation of the document
EDIT2
I just tested it and retrieving the value works just fine, so the problem must be in your document writing method, please check this documentation it can help you and i think is a better solution:
http://www.html5rocks.com/en/tutorials/file/filesystem/

Find script that changed the value attribute of a <input /> tag

<form id="search" action="/search" method="get" autocomplete="off">
<div>
<input type="button" name="test_button" value="test" />
</div>
</form>
<script>
document.getElementById("test_button").value = "changed_test"
</script>
Just as the HTML code above shows, I have defined a button with name test_button and value test and changing its value with the code in the script tag.
Now I am debugging a large webpage which is using a mechanism like this using Firebug and Firefox in Linux.
I want to know how I can find the script that changes the value attribute of the <input ... />, but the web page is too large, various <script> and anonymous functions which are auto-executed made it nearly impossible to find the specific script manually.
Since I am in Linux, I cannot use any Microsoft tools to search the whole web page. I only have Firebug and Chrome. Can Firebug realize that? Does anyone have a good idea of how to find the specific <script> that changed the value?
Add some code like this to the document, right after the form with the button:
<script>
var node = document.getElementById("test_button");
Object.defineProperty(node, 'value', {
set: function() { throw new Error('button value modified'); }
});
</script>
This will throw an error when anything tries to modify the button's value.
Expand the error and click the last line number shown. This will take you straight to the line that set the value of the button.
Here's a demo: http://jsfiddle.net/XSJZN/
Tested in Chrome 17.

Grails and Javascript

I would like to know how to use tinybox in my Grails app.
I've tried this so far (placed it inside the head tags):
<g:javascript library="TinyBox/tinybox"/>
<g:javascript>
function errorResponse(){
var b = ["Oh com'on!Say it!",
"At least say a word!",
"Don't be mean, say something. :)",
"I'll be sa sad."];
var a = Math.floor(Math.random()*3);
//alert(b[a]);
TINY.box.show(b[a],0,0,0,0,3);
}
</g:javascript>
And I called it somewhere inside my form:
...
<g:submitToRemote update="comments" url="[controller:'sushiTrail',action:'save']" value="Send" onFailure="errorResponse();" onSuccess=" comment.value='';"></g:submitToRemote>
...
I'm new to both grails and Javascript. Tinybox is supposed to load when validation fails. I've tried a simple alert() and it worked, and I seriously think there is something wrong with the way I call stuff. I followed the tinybox demo here, but I still can't get it going even though view source seems to be correct. Can someone enlighten me?
Note: there was no error. The tinybox just didn't appear. :(
The <g:javascript> tag only accepts values "prototype", "scriptaculous", "yahoo" or "dojo" for the library attribute, so the following won't work:
<g:javascript library="TinyBox/tinybox"/>
Add the tinybox JavaScript files to the Grails application's web-app/js directory and use one of the following instead to import it:
<g:javascript src="path/to/tinybox.js" />
or
<script type='text/javascript' src="path/to/tinybox.js"></script>

Categories