I have one javascript error in my IE8 [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
"Object doesn't support this property or method"
It's this line.
pthumb = $("#pthumb").attr("src");
Does anyone know why?

You have a javascript variable called "pthumb" and a DOM element with the id "pthumb", and IE's JS engine could be trying to use the wrong one.
If you have a function also called "pthumb" then IE could also be trying perform this action on the function object.
The last thing to try is to make sure you are using "var" when declaring "pthumb" in the Javascript. i.e.:
var pthumb = $("#pthumb").attr("src");

Are you ensuring the DOM is ready?
$(document).ready(function(){
//wrap your code in document-ready check
pthumb = $("#pthumb").attr("src");
});

you could double check the plain javascript method:
var jthumb= document.getElementById('pthumb').attributes['src'].value;
try{
pthumb = $("#pthumb").attr("src");
}
catch(er){
alert(er.message + '\n'+jthumb)
}
If you don't catch the error, the element is not yet ready.

Related

Using innerHTML for partial HTML [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
the following function
<script>
function new_par()
{
var beginning="<input id=";
document.getElementById("debug").innerHTML=beginning;
}
</script>
produces no output, although if I remove the "<" sign, it does. Presumably the javascript avoids an output that would destroy the html page, but would there be anyway to force the output?
You're assigning it to a node's innerHTML property. When you plug the string into the DOM it's trying to parse it as though it were HTML. If you were to simply add it as a text node it would appear.
var beginning="<input id=''";
document.body.textContent = beginning;
https://jsfiddle.net/2patzyo1/
Edit: upon looking again at your question, if you are trying to get the input element to appear on the page, the string you're using isn't proper HTML because you never closed the tag. When the browser runs into it, it tries to parse and render it, but because it isn't valid it gives up. This will work:
var beginning="<input id=''>";
document.body.innerHTML = beginning;
https://jsfiddle.net/2patzyo1/1/

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Is this eval Evil? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Writing a function in JavaScript. The plan is the function creates an object, which requires boolean statements as parameters. Something like this ->
var foo = new fuzz("pie < squirrel", "monkey === banana");
My question is - Is this evil?
*Note - * Inside the function 'fuzz' I will run checks on the values of the parameters. (Check string.length etc). I think this is how one is meant to use eval, it just has such a bad reputation on t'up web.
Thanks
Summing up the conclusions in the comments: write a simple rule evaluation engine! E.g.:
var variables = { ... };
function niceEval(condition) {
var operands = condition.match(/(\w+)\s+(\S+)\s+(\w+)/);
switch (operands[2]) {
case '<' :
return variables[operands[1]] < variables[operands[3]];
...
}
}
This also gives you a lot more control over possibly occurring errors than blindly evaling a string.

Jquery load() use variable instead of a url [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I use the jquery to load a page into a div. like:
#('#mydiv').load("http://gowemto.com/index.php");
I want to store the url into a variable like this:
var myload="http://gowemto.com/index.php";
but when I try to call this function #('#mydiv').load(myload");
it doesn't work :(
any help? I really need to use variable instead of url inside my function
There is a typo, you have to remove the extra ", at the end of the variable myload; try:
$('#mydiv').load(myload)
if you are storing jQuery in # var (eg noConflict), but I don't think so, your code must be:
#('#mydiv').load(myload)
You have a syntax error (extraneous " after the variable name), and you should use $ instead of #:
#('#mydiv').load(myload");
It should be:
$('#mydiv').load(myload);

Get the value of a div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Categories

Resources