javascript function not working with rails - javascript

I'm facing a problem: I have a javascript function in my application.js but it's not working correctly. Always when I click in the button, it gives me this error in the firebug:
goto_nex_photo is not defined
but I have this function implemented in the application.js. I don't what is going wrong. I will post the code of application.js and the gallery_detail.html.erb:
https://gist.github.com/902973

Line 600 you have a random F at the start of the line.

That is a lot of code to work though. To much for me but I can offer you a method to find what is broken. Mostly likely there is something wrong with some piece of code above that function in the javascript.
Start by moving that function to the top of the script... ... then move it subsequently down ... one method at a time until you find the method that had the bad code...
Another thing you could do is look at the RAW html actually outputted and make sure that the javascript include for the function is there... I did actually check your code and it looks like all your curly braces match up...

Related

Unexpected token on line 29

My friends and I are trying to make a website from scratch and we have barely ever touched html. We have got a private domain, but we just started and don't seem to know how to define this unexpected token found on line 29. Any help would be well appreciated. This is the only thing limiting us to run our code (I think).
Sign out
This is what pops up whenever trying to run the code:
Picture of code on line 29:
line 28-44
Based on the images given (more would be nice, or code samples), I can see the function signOut() being called, but I can't see a { after it. Inside your <script> tags, the function call should be function signOut(){ //Your code }. Please see if it works.
In addition to this, I'd check that all tags are ended (for example <script> should have a matching </script> tag after the necessary JS). As others have said, as well, make sure the file ends with .html!

How to run a function using "onclick" in JS "setAttribute"?

Please see my code here: https://repl.it/#AliMosallaei1/Just-A-Test (it uses localStorage and has a lot of parts so I can't really put it in the SO sandbox)
When you add a class and click on the Name of the class, it should execute the edit() function in grade.js. However, instead, it is throwing a TypeError, saying edit is not a function. What could be causing this issue?
EDIT: The file that needs help is the grade.js, just to clarify. The major things to look at are the edit(i) function and the big for loop.
This answer should be on the comment section but I don't have the enough reputation.
Try renaming your "edit()" function to "edit2()" or something else, this should work.

How do I use jQuery.noConflict();

After installing lightbox for my website my comments stopped working. I looked at the code and moved the javascript links that were in the <head> (the ones that came with lightbox) in the to above the javascript links that came with Wordpress-Buddypress. Before my javascript links from Lightbox were below the javascript links that came with Wordpress.
After making the switch, the comments started working again on my website but now the lightbox does not work.
When I use Firebug to find errors, I get this error.
$("#videogallery a[rel]").overlay is not a function
That code comes from "videolightbox.js" which was a file that came with my lightbox.
I did lots of reseach on this problem and I am thinking that I might need to use jQuery.noConflict(); but I have no idea how to use it? I was looking at this link
but I can't seem to get it to work because I have no idea how to use it. I also tried replacing all the $() with jQuery() but that did not solve my problem.
but I have no idea how to use it?
var $j = jQuery.noConflict();
and your code will change from
$(selector) to $j(selector)
A lot of examples are given here
http://api.jquery.com/jQuery.noConflict/
One thing to consider closely is the script loading order and watch for repeat loading of JQuery. In some (hopefully rare) situations, you can load JQuery, set up some code that uses it, then reload JQuery which tosses away the events that were set up after loading it the first time.
I know I ran into that situation once, but I can't remember the details that led to it occurring.
To your answer
$=jQuery.noConflict();
Now you can use $('#id').css(...);
The error is probably causing the rest of the code not being executed, .overlay is not a function means that .overlay is not a function and $ is working fine.
var test; test("#videogallery a[rel]").test()
//TypeError: test is not a function <-- You would see this if $ wasn't working
var test = function(){return {};};
test("#videogallery a[rel]").test();
//TypeError: test("#videogallery a[rel]").test is not a function <-- You see this because .overlay isn't working
It simply means you are trying to call .overlay before it exists.

Can't get a Javascript function named "switch" to execute

I have a simple script that should cause one of three divs to be visible while the other two are not. The function that does the work is called like so:
onchange="switch(this);"
Firebug indicates that there is an error with this text:
Javascript Error: missing { before switch body
The erroneous code it indicates is line one of my .php file where the doctype is defined like so:
<!doctype html>
The funny thing here is that I have another page with the same doctype and a script that is virtually identical which works 100%. The only differences between the two pages are that in the one that does work, I call the script from
One more thing about the Firebug output: On the page that works, the firebug script window shows the javascript like so:
function onclick(event) {
switch(this);
}
Now, on the page where the script doesn't work, Firebug shows no output that has anything to do with onchange, onclick, or anything else. It just shows the code from my javascript file and tells me I am missing the opening bracket to the function when it is clear as day that it's there. Perhaps, even with the script in the head of my main php file, something odd is happening with scope, making the defined function invisible to the callers. Any ideas?
1: why would Firebug tell me the error is on line 1 where the doctype is defined when the function that fails isn't even in the same file?
2: Does the doctype effect the way that javascript runs, and how do I debug it if it does?
I would prefer to continue using only HTML5 for this project and use a javascript file for backwards compatibility. Any help is very welcome!
P.S. I am running Ubuntu 11.10 with Apache2, PostgreSQL, and PHP5. Everything works perfectly outside of this one javascript issue.
EDIT: Totally stupid question, but I guess these things happen sometimes. As stated in the answers, switch is a keyword in Javascript and changing the name of my function fixed the problem. I really should have noticed that since my editor highlights keywords in brown...
I am not deleting this post (unless someone else suggests I do) in case someone else out there runs into the same problem. I am giving the answer to the guy who answered it first because his answer also explained the reason why I was getting the error messages I was getting, which is probably more helpful in the long run than a simple awareness of switch statements.
This error has nothing to do with your doctype or HTML5. It occurs because switch is a reserved word used for switch statements; you cannot name a function switch.
So when you do switch(this) the JavaScript engine is expecting you to follow that up with the rest of the switch statement, including the opening {, the switch body, and then the closing }. When you don't do that, it throws the given error.
The error is on "line 1" because you used an inline event handler, which in Firebug's mind is a JavaScript file with one line---that line simply being switch(this);. Firebug does not deal in line numbers of HTML files, only those of JavaScript files---whether they be real JavaScript files, or "virtual" ones generated by inline event handlers.
switch is a keyword in javascript, rename your function to something else like myswitch.
switch is a keyword in Javascript

Run sifr without page refresh?

is it possible to make sifr re-run or update again without having to refreshing the page??
Id imagine there is a function like sifr() or something..?
Which version of sIFR? At least sIFR 3 has a method for this:
sIFR.redraw();
You are spot on: There is a function, called sIFR().
The Javascript replacement statements can be added in the sifr.js file, or at the end of your (X)HTML file.
When you put the replacement code in the sIFR Javascript file, it’ll execute on onload (depending on if sIFR.bAutoInit is set to True, which is the default value) or when you call sIFR(). If you put the replace statements in the body, they'll be executed immediately.
Effectively, this means that you could put the replacement statement in the JavaScript and call sIFR() in the body. It won't make any difference.
To save bandwidth you can put the replace statements in the JavaScript file, and then call sIFR() in the body. The exact code you have to use in the body in this case is:
<script type="text/javascript">
if(typeof sIFR == "function"){
sIFR();
};
</script>
Obviously you could call this function later again, e.g when something has changed asynchronously.
Sidenote: I wasn't aware of the redraw() function opted by Simon. Obviously I would give that a go, if I where you.
thanks guys. unfortunately for me its version 2.0.7. I'll have to let the front end designers know to update sifr for future markups. I havnt got time to play with this anymore as there is more important things on the project.
I tried the whole sIFR() thing, but didn't work, looks like ill have to refresh the page everytime the user switches themes. Or if i get time towards the end ill update it to v3 and try the redraw().

Categories