Use start? method from opal-browser - javascript

I am trying to translate this js code into ruby code
document.ontouchstart ? 'touchstart' : 'click';
I am using opal-browser to get browser functionality. My current attempt is this:
touch = Browser::Event::Touch.new
puts touch.start?
However this returns the error:
Uncaught TypeError: Cannot read property 'type' of undefined
When I checked the error trace the error seems to be coming from a missing name property in the Touch class.
Hoping Opal community can help me out here

If you want to check if touch is supported you need to call Event::Touch.supported?.
For the error, it's happening because .new expects the event object as parameter, if you want to create a new event object you need to call .construct.
In retrospect it would have probably been better to have .construct be .new, and .new be .wrap or something on that line.

Related

Don't add methods to the wrong prototype

I'm using jQuery with an Electron app, but I always get an error that seems to be corresponding with jQuery's tween function.
I'm loading jQuery via standard node require:
<script type="text/javascript">window.$ = window.jQuery = require('jquery');</script>
When I include jQuery via a script src, I get the same error (jQuery version 3.3.1)
for example calling $("#loading-overlay").fadeOut(200); causes:
Uncaught TypeError: (Animation.tweeners[prop] || []).concat is not a function
at createTween ([PATH]/node_modules/jquery/dist/jquery.js:6848)
at Object.defaultPrefilter ([PATH]/node_modules/jquery/dist/jquery.js:7021)
at Animation ([PATH]/node_modules/jquery/dist/jquery.js:7160)
at HTMLDivElement.doAnimation ([PATH]/node_modules/jquery/dist/jquery.js:7293)
at Function.dequeue ([PATH]/node_modules/jquery/dist/jquery.js:4376)
at HTMLDivElement.<anonymous> ([PATH]/node_modules/jquery/dist/jquery.js:4418)
at Function.each ([PATH]/node_modules/jquery/dist/jquery.js:354)
at jQuery.fn.init.each ([PATH]/node_modules/jquery/dist/jquery.js:189)
at jQuery.fn.init.queue ([PATH]/node_modules/jquery/dist/jquery.js:4411)
at jQuery.fn.init.animate ([PATH]/node_modules/jquery/dist/jquery.js:7304)
I'm only having this problem with Electron (version 4.0). Does anyone know what this is caused by?
OK, this was stupidity on my part. Autocomplete made me add the method "each" to the Object prototype instead of my custom class... This apparently confused jQuery because it found the property "each" in Animation.tweeners (because it was in all Objects).
But I learned, when facing a really confusing error that makes no sense, check if you accidentally overwrote a prototype you didn't want to change...

Firefox event.clientX not functioning

I'm working on a d3 and js project.
The beginning of the function looks like:
$(document).ready(function() {
d3.select("#aid").select(".abutton").on("mousemove",function() {
afile.style("top", (event.clientY+10)+"px").style("left",(event.clientX+15)+"px");
afile.html("<h3>Click text here</p><p>or here</p>");
});
I've done quite a bit of Googling!
The essence is that on mouseover, it should do the function. This works in Chrome and IE because the event variable is global, and as such so are it's client* properties.
The solution, as I understand it, is to pass in an eventObject. When I do that, my code looks like:
$(document).ready(function() {
d3.select("#aid").select(".abutton").on("mousemove",function(event) {
afile.style("top", (event.clientY+10)+"px").style("left",(event.clientX+15)+"px");
afile.html("<h3>Click text here</p><p>or here</p>");
});
The Firefox log gives me:
[09:59:04.308] TypeError: event is undefined # filepathofjavascriptfile
and similarly, it breaks in Chrome:
Uncaught TypeError: Cannot read property 'clientY' of undefined filepathofjavascriptfile
(anonymous function) help.js:34
What am I doing wrong? Please let me know if you need anything else.
Try:
d3.select("#aid").select(".abutton").on("mousemove",function() {
afile.style("top", (d3.event.clientY+10)+"px").style("left",(d3.event.clientX+15)+"px");
afile.html("<h3>Click text here</p><p>or here</p>");
});
For whatever reason, that's how d3 exposes the event object.

Uncaught TypeError: Cannot read property 'value' of null, need some direction

I have been trying to figure out this particular problem in my developer tools, but I've had no luck thus far. I have an error on one of my js files that says
Uncaught TypeError: Cannot read property 'value' of null
The following error refers to the 1st variable of dt_version below. The particular thing is if I comment out the first line of code. I get the same error on the following variables of offload1 and offload2. The variable is a number that I am trying to get passed over. I run this function on my body when the page loads...onload=updatetotal();
function updatetotal() {
var dt_version = document.getElementById("dt_version").value-0;
var offload1 = document.getElementById("capacity_offload1").value-0;
var offload2 = document.getElementById("capacity_offload2").value-0;
var offload3 = document.getElementById("capacity_offload3").value-0;
}
If a run an if statement looking for document.getElementByID("dt_version");...it defaults to false..so its not being carried over though on the previous page, I can see its input fine with the value in it. What am I missing here guys?
This error means that the id dt_version does not exist. Check your html to make sure it is there:
var dt = document.getElementById("dt_version");
if (dt){
// do your stuff
}else {
console.log("dt does not exist")
}
Another cause for this error may be- as you are calling the javascript function on page load there is a possible chance that your control is not yet completely rendered to the page. A simple solution is just move that control to the beginning of the page. If it doesn't work then an reliable solution is, call the function inside jquery $(document).ready().

Undefined variable error that shouldn't be a variable

I've made a piece of code in jquery that assigns a href attribute to a variable. Here's that code:
$('#reactions' + i).attr('href', 'javascript:comments ('+entry.url+','+i+');');
This should assign a call to the javascript function comments. Now I want to use that call on a jquery mobile button, like this:
document.write('Reactions');
But doing this gives me a in FF and Chrome. This is the error from FF±
Uncaught exception: ReferenceError: Undefined variable: i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what
Error thrown at line 1, column 0 in javascript:comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);:
comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);
In this, i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what is the value of entry.url.
I'm just not getting why this error appears, as far as I know, everything should work.
I know that there are questions looking similar to mine, but I couldn't figure out the answer. If you want to see the whole source, it's here.
Surround entry.url with quotes:
$('#reactions' + i).attr('href', 'javascript:comments ("'+entry.url+'",'+i+');');
The best way to fix the issue is to do it the "jQuery way". Instead of adding a href attribute that executes JavaScript, add a click event:
$('#reactions' + i).click( function() {
comments( entry.url, i );
});
Similarly don't use document.write() but add elements to the document using jQuery functions.

Javascript error - SXBB[id].resize is not a function

My forum (based on phpbb3) has a Javascript error that I'd like to resolve. In FF and IE the following error occurs:
Error: SXBB[id].resize is not a function
Source File: http://digital-diy.com/forum/classes/scripts/select_expand_bbcodes.js
Line: 197
The mod that uses this script is called "Syntax Highlighter 1.0.15". The developer is not sure why the error occurs, hopefully someone at stackoverflow can lend a hand?
Track down the SXBB object or array and view the id variable.
Make sure that property (what id refers to) is set on that object (SXBB).
My guess is it isn't, and it's undefined, and undefined has no resize() method.

Categories

Resources