I have a php site that works fine in FireFox and Chrome, but breaks completly in IE.
Here is just one of the scripts that is throwing an error...
SCRIPT600: Invalid target element for this operation.
function loadDeals() {
$.get("modules/recommendations/viewrecommendations.php",{},function(response){
document.getElementById("dealdata").innerHTML = response;
});
}
It throws the error on the line that sets the innerHTML...Any ideas why this is happening?
IE has a problem replacing TBODY contents with innerHTML. The jQuery given above works; if you are not using jQuery, another solution is to have a <div id='helper' style='visibility:hidden'/> somewhere in the page - when the response arrives, put the value with a surrounding <table> tag into the hidden div, then use the DOM to remove the old contents from your visible tag and insert the elements from the hidden tag 1 by 1:
var a=document.getElementById("dealdata");
while(a.firstChild!=null)
a.removeChild(a.firstChild);
var b=document.getElementById("helper");
b.innerHTML="<table>"+this.responseText+"</table>";
while(b.tagName!="TR") {
if(b.tagName==null)
b=b.nextSibling;
else
b=b.firstChild;
}
for(;b!=null;b=b.nextSibling)
a.appendChild(b);
Try this: are you using jquery?
also looks like you have an extra set of brackets in there (i think between ,{},)
function loadDeals() {
$.get("modules/recommendations/viewrecommendations.php",function(response){
$("#dealdata").html(response);
});
}
Related
I'm using this code to insert before all elements in a specific div.
$.post('getcomments.php', {rid:1, frm: 10,to:20}, function(data) {
$("#main_post_comments").before(data);
});
the first time the page loads the function works fine
but after that is always append before the original html element that was written in the original html code not the last appended element that I requested with the $.post
this is the first time the page loads (firs item is comment 102):
This is after inserting the first comment (comment 103 is insrted after 102) correctly:
This is after inserting the second comment (comment 104 also inserted after comment 102)
How to insert the new data before every element in the "#main_post_comments" even if it is inserted dynamically?
Use prepend instead of before
$("#main_post_comments").prepend(data);
Try this:
$.post('getcomments.php', {rid:1, frm: 10,to:20}, function(data) {
$("#main_post_comments").insertBefore(data);
});
Maybe you are looking for insertBefore();
http://api.jquery.com/insertbefore/
Ok so I finally figured out which part of my code is causing the exception. You can read the initial post here. The code in the initial post is missing the part which is actually causing the exception (the manual subscription to the viewPortData observable). Apparently, I'm doing it wrong somehow... Here's the code:
self.viewPortData = ko.observable();
self.viewPortData.subscribe(function (newValue) {
var viewPort = $('#metro-view-port');
if (viewPort && newValue) {
self.fadeInOut(viewPort, newValue);
}
});
self.fadeInOut = function (domObject, newContent) {
if (newContent) {
var currentContent = domObject.html();
if (currentContent) {
var wrappedContent = $(currentContent);
wrappedContent.fadeOut(400, function () {
wrappedContent.empty();
domObject.html(newContent).hide().fadeIn(400);
});
} else {
domObject.html(newContent).hide().fadeIn(400);
}
}
};
So where did I go wrong?
The same error occurred to me. The problem was caused because the HTML had comments on it. Something like:
<!-- Some Comment goes here -->
<div>
...
</div>
To fix that, without changing the HTML, you need to wrap the HTML with something else, so you pass only one element to jQuery:
var div = document.createElement( 'div' );
div.innerHTML = nativeHtml;
var $html = $( div );
I created a fiddle using your code from this post and the previous post, and it works as it should.
However, I'm only returning a simple <div> tag to populate the HTML of the metro-view-port <div>.
My best guess is that the HTML that you're returning is the problem.
My advice to you is to first confirm this by reducing the HTML returned to something very simple, and then gradually reintroduce the intended code until you find the problem.
Flip your fadeIn(400) to a show().
It is simpler for jQuery to do the math for.... I think that it can't get computed style of the elements due to some floats inside it or something.
I had the same problem..... but after some research I got to here (DAMMET I LOST THE TAB - it was a jQuery bug report anyway ) and realised what needed to be fiddeled with to fix it.
In my code I swapped out fadeIn() to show() so it isn't to do with the animation
you would have thought that without the animation the problem wouldn't be prevalent either - but it is.
try slideDown(0 if your still after an animation, it might not work but its worth a pop.
This bug was in old versions of jQuery. Try to change .hide() to .css('display', 'none')
According to this jQuery bug, the problem may have to do with newline characters and whitespace text nodes in your HTML. In my case, I was taking a template like this one:
<script id="myTemplate" type="text/template">
<div>
<h2>Important stuff</h2>
</div>
</script>
And parsing it like this:
var currentContent = $.parseHTML($('#myTemplate').html());
So I ended up with a bunch of text nodes representing the newline and whitespace characters in the original HTML template. Probably something similar has happened to you.
To fix this, I stripped out the newlines and whitespaces like so:
$('#myTemplate').html().replace(/\n/g, '').replace(/>\s+</g, '><').trim();
Hope that helps someone!
I get form from zend framework site and put it in response in new file in function written by jquery mobile, but I get this error:
uncaught exception: cannot call methods on selectmenu prior to
initialization; attempted to call method 'refresh' .
Code of function this file:
function addItem(id) {
$.ajax({
url:'http://zf.darina.php.nixsolutions.com/order/index/create-order-mobile',
dataType:"jsonp",
data:{id_good:id},
success:function (resp) {
console.log(resp);
$('.product-table').empty();
$('.product-table').append(resp.prod);
$('.product-table').append(resp.form);
$('.add-order-btn').button();
$('.mob-size').selectmenu('refresh', true);
$('#block').page();
}
})
}
Force initialize the selectmenu(s) first:
$('.mob-size').selectmenu(); // Initializes
$('.mob-size').selectmenu('refresh', true);
or use this for short
$('.mob-size').selectmenu().selectmenu('refresh', true);
In my case, if I was not initializing the select before invoking the 'disable' method I got the error, while if I was initializing it, the select didn't disable but duplicate itself - I tried to select the object by TAG NAME instead of by CLASS or ID NAME,
$('select').selectmenu('disable');
instead of
$('.select-class-name').selectmenu('disable');
and it worked without forced initialization
you do this in your custom refresh delegation function:
var w = $("#yourinput");
if( w.data("mobile-selectmenu") === undefined) {
// not initialized yet, lets do so
w.selectmenu();
}
w.selectmenu("refresh",true);
according to enhancement resolution here
I found the same problem, but a more involved solution. When jqm wraps the select element, it puts it in a <span> element with the same class list as the select element. I changed my reference to it so that instead of reading:
row.find(".selectCompletion").selectmenu("disable");
it now reads:
row.find("select.selectCompletion").selectmenu("disable");
Specifying that jquery should only find the select element matching the class name, rather than all elements in .row that match the class name, solved the problem.
This happened to me when cloning existing select element in order to duplicate the original section multiple times.
Calling 'refresh' for the original element, worked fine, while calling it for the cloned sections was leading to the error appearing in the question.
However, calling selectmenu() was causing a 'vandalisation' to the form, as can be seen in the following image:
Explanation: top = original. bottom = vandalised cloned element right after calling selectmenu.
Solution:
The following code solved this vandalisation problem:
cloned_elem.find('select[name=MyClass]').selectmenu().selectmenu("destroy").selectmenu();
This is not an ideal solution because we must call the first selectmenu() in order to call selectmenu("destroy"), so I would be glad to hear of a cleaner solution.
I am using this JavaScript code to remove a couple elements from the page, but it's not working. When I inspect the code with Opera Dragonfly it says something like:
Uncaught exception: Error: WRONG_ARGUMENTS_ERR
and points to the file and function name.
The weird thing is that I use the exact same code in another function on the same page and it works without problem. The code is very small and simple:
var docBody = document.getElementById("body");
if(document.getElementById("marginDiv")){
docBody.removeChild("marginDiv");
}
Both body and marginDiv exist on the page. My goal is to make the thumbnails disappear when one clicks the background.
You're trying to remove a string. A string is hardly an HTML element. You're also relying on marginDiv being a direct child of body, which may not be the case.
Instead, try this:
var remove = document.getElementById('marginDiv');
if( remove) remove.parentNode.removeChild(remove);
Try
docBody.removeChild(document.getElementById("marginDiv"));
removeChild needs a reference to a DOM element, not a string. Try this:
var docBody = document.getElementById("body");
var marginDiv = document.getElementById("marginDiv");
if(marginDiv)){
docBody.removeChild(marginDiv);
}
if(document.getElementById("marginDiv")){
docBody.removeChild("marginDiv");
}
you have to check if specified element exist marginDiv exist, then removechild(...)
I want to update the contents of a TBODY (not the entire TABLE, because there's much more semi-meta data (LOL) in that). I get >= 0 TR's from the server (XHR) and I want to plump those in the existing table. The fresh TR's must overwrite the existing TBODY contents.
I've made a very simple, static example on jsFiddle that works in Chrome and probably all the rest, except for IE (I only use Chrome and test in IE8).
In Chrome, the very first attempt works: plump the TR's in the TBODY. No problem!
In IE it doesn't... I've included a not working example of what I had in mind to get it working.
I'm sure this problem isn't new: how would I insert a string with TR's in an existing TBODY?
PS. jQuery doesn't have a problem with this!? It's used here on SO. jQuery does something to the HTML and then inserts it as HTML nodes..? Or something? I can't read that crazy lib. It happens in this file (look for "html: function(". That's where the magic starts.
Anybody have a function or idea for this to work without JS library?
Here is a good resource about the problems of innerHTML and IE.
The bottom line is that on tbody the innerHTML property is readonly.
Here is a solution presented in one of the comments:
var innerHTML = "<tr><td>Hello world!</td></tr>";
var div = document.createElement("DIV");
div.innerHTML = "<table>" + innerHTML + "</table>";
// Get the tr from the table in the div
var trElem = div.getElementsByTagName("TR")[0];
Regarding the jQuery part of the question:
//inside the html() function:
// If using innerHTML throws an exception, use the fallback method
} catch(e) {
this.empty().append( value );
}
//inside the empty() function (basically removes all child nodes of the td):
while ( elem.firstChild ) {
elem.removeChild( elem.firstChild );
}
//append calls domManip applying this to all table rows:
if ( this.nodeType === 1 ) {
this.appendChild( elem );
}
//domManip as far as I can tell creates a fragment if possible and calls the three lines above with this=each row in turn, elem=the tbody(created if missing)
Using plain JavaScript, you can set the innerHTML property of the relevant element. The text that you set can contain a mix of HTML and text. It will be parsed and added to the DOM.