We all know (and love!) the fact that Mozilla exposes element constructors (HTMLParagraphElement, for example) for prototyping. Turns out Opera 8 allows you do to the same thing.
So this leaves Safari, which has actually allowed you to do this since 1.0, *but* doesn't publicly expose the constructors. The below code exports constructors into public variables matching those in Mozilla and Opera 8:
/*
HTMLElement Prototyping in KHTML and WebCore
Copyright (C) 2005 Jason Davis, www.jasonkarldavis.com
Additional thanks to Brothercake, www.brothercake.com
This code is licensed under the LGPL:
http://www.gnu.org/licenses/lgpl.html
*/
function HTMLElement() {}
HTMLElement.prototype = HTMLHtmlElement.__proto__.__proto__;
var HTMLDocument = document.constructor;
var HTMLCollection = document.links.constructor;
var HTMLOptionsCollection = document.createElement("select").options.constructor;
var Text = document.createTextNode("").constructor;
var Node = Text;
}
In Opera < 8, all elements inherit directly from Object(), so you can still prototype Object() if you need to do element prototyping. Internet Explorer's elements *don't* inherit from Object, oddly enough, so who knows with that browser. In any case, there you go. It also happens to work in Konqueror (AFAIK).
In Javascript if I have an HTMLElement object, for example as returned by a call to document.getElementsByTagName or document. getElementById,is there any way I can tell whether that page element (eg paragraph, image, or DIV tag) is currently selected on the page ?I know there are functions such as window.getSelection which can query selected text. However what I would like to be able to tell is whether an arbitrary element represented by a JavaScript HTMLElement object is part of the current selection that the user has made on the web page.ie. this would cover any type of element not just text.Are there any other possible ways to achieve this, eg JavaScript calling into a Java program for example ?
When I use this function, one of the keys in the iteration will be the new function that I have created.
The following example would alert the following: "key1", "key2", "keyExists". The last one is my concern. Obviously their are other browser-defined functions available for Objects that don't show up when iterating through the object. I thought Object.prototype.functionname was the methodology for that but apparently not. Is there a way to achieve the effect I am looking for? Code:
I am ok with using objects creating classes if someone else defines, but when it comes to defining my own, I hit a nasty brick wall... I am using an XML/XSLT wrapper called Sarissa to help with programming a utility to transform XML into HTML in different views. For this to happen, I have created a Loader class which loads in XML required. I am aware of prototyping for binding methods to objects (as opposed to replicating the same method every time an instance is created)... The aim being I want to create a progress bar for the essential files that need to be loaded in. Presently I have them load in Synchronous mode just to get the utility working, which I know is poor, so would like to address it.
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.)
The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple instances without prototyping. I've seen (and used) JSON for singleton object instances -- this not a problem and this is how it works right out of the gate.
But given the following custom object written the past "normal" way, I would like to write it in JSON format, and then be able to create new instances from the one definition. Here's an example using the "old way" most who have been writing JavaScript for years have seen:
But neither are really as close to pure JSON as I would like, so that I can instantate those:
var hisPair = new Two( 11, 22 ); var herPair = new Two( 33, 44 );
What I'd like is a way in PURE JSON to be able to create the Two class (as an example) using pure JSON. I've not seen anything yet on the web that addresses this directly aside from some pages which require you to include another JS to allow "deep embedding" of classes using other helper "classes" (that are created the "old way" it seems), etc.
The best I've found so far on using pure JSON to create a class that allows *multiple* instances is something like this:
function Two( x, y ) {
var class = { x : x, y : y, sum : function { return this.x + this.y } max : function () { return this.x this.y ? this.x : this.y } min : function () { return this.x this.y ? this.y : this.x } pow : function () { return Math.pow( this.x, this.y ) } };
for (var element in class) this[element] = class[element];
}
Now *THAT* works, but it's still not as "pure" I would like. But it's acceptable for now, I guess, since I *am* creating the entire "class" as a JSON object, and I consider the outside function "wrapper" as the necessary "constructor." But I keep wondering... There HAS to be a better way.
I'm just wondering if anyone knows of a place that discusses JSON used in situations like the above. Again, I've seen an ABUNDANCE of pages and sites that discuss JSON in Singleton usage, but nothing that discusses it as I am wanting here.
I'm apparently misunderstanding what I'm reading on prototyping. My main task is to squeeze some performance out of an app that's a bit slow on IE, and it looks like large Arrays and their overhead may be part of the problem. I'm trying to replace those with my own array type based on Object and extend it with helper functions like .length. Here's a munged sample of what I've tried:
I have a few String prototypes such as String.prototype.EscapeReg = function () { return this.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&"); }; // Escapes characters for use with a regular expressionI also have my own class/ library which is used like this var adg = new AndyG_ns.ADG_Utils(); adg.StartClock('AndyClock','dd mmm yy hh:nn'); // etc.What I would like to do is to only add the prototype to my library (not to the global namespace). The end result I'm looking for is to use code such as:
var adg = new AndyG_ns.ADG_Utils(); var myString = new adg.AString(); var parsed = myString.EscapeReg();
In addition, I want to be able to also use/create my special string sub-class within my library. I suppose I'm saying that I would like to extend or super-class the native String object.
I'm trying to create a Youtube video inserter script for my site. Long story short, I need to be able to prototype an HTML element and add a string into the element's innerHTML, like this:
I am working on a rapid html prototyping framework. This would allow me (as an interaction designer) to create html prototypes fast.I have been looking in templating plugins to help with building the templates or filling them with content. For example {{ lorem_ipsum }} would be replaced with a few paragraphs of random text. This is easy to do with several plugins.
However it would be great to be able to add a bit more flexibility by using functions like {{ loremWords(100) }} which would add 100 random words which are different for each replaced item. Or something similar to add a menu or other snippets.It is probably not too difficult but so far I have not gotten it working yet. And the pluginsI have found are either too complex, requires script tags or can't handle functions.
The very simple code (below) works fine in Safari (and Chrome, Firefox etc.), but it doesn't work in Mobile Safari. Why?(You can find a working example at: http:[url]....)
Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>[code]......
Does anyone know if there is a quick test to tell if Safari 1.3 (or 2.0 will suffice) is the browser that doesn't rely on the typical navigator.* methods? For instance, there is the good 'ol document.all test for MSIE... Looking to add Safari support to my tty editor for my cms.
I am currently working on a project that displays preview of a jpeg in an iframe. we can edit this preview - like increasing its zoom level and changing pages , images etc. This works fine with IE on windows but whenit comes to Safari on Mac , the preview is lost on refreshing the browser.
I have noticed that this problem occurs when ever I use Iframes. Although the main jsp page is refreshed and shown, the jsp page that is the source for iframe contained in the main jsp page is not shown.
that some_script would not be called until the <body> was completely loaded - is this not the case? With Safari 1.3 I seem to have to delay inside some_script (there is some php in the <body> that slows down the loading). Since I happen to have a spare iframe in my <body>, I load a tiny bit of html in it whose job is simply to set a "loaded" flag, tested inside my delay code.
What I was observing was that some fields inside a <form> in the <body>, whose values are set by some_script, were, with Safari, not visible until I clicked in one of them - then they all popped into sight. I wasn't seeing this with other browsers and a delay mechanism fixed it.
It was as if the onload was triggered as soon as it was encountered rather than when the loading was complete.
I am having problems with a website. It uses javascript to choose a payment option and then calculates the price accordingly. It works on I.E and firefox, but not on safari. A few of our clients use it so it has to be sorted out urgently.
I have no idea why it wouldn't work in safari and i don't have broad enough skills for that.
I have a form that has a Javascript function being called when a button is clicked. The function works properly in every browser except Safari and IE 5.2 for Mac. Any ideas?
I'm writing some stuff where I wish to allow the cursor keys to control elements in a page. This has not been a problem except with Safari which appears to duplicate the keydown and keyup events which are fired when the cursor keys are pressed. I.e. pressing and releasing say, K, results in one keydown event followed by one keyup event. Press any of the cursor keys results in two keydown events followed by two keyup events.....
I have a big <table> and I have added an onmousedown handler. When I get back the event in IE and Firefox, the individual <td> element appears in window.event.srcElement (IE) and in event.target (Firefox). However in Safari the target is just the <table>, not the <td>.
I need to get back the <td> associated with the event, and I would not like to put an onmousedown handler on each table cell because there are a lot of them. Does anybody know how I can get the event and figure out which table cell was clicked?
The stuff I've built recently works (in the sense that it does what I'm expecting it to do without any errors or warnings) in IE and FF but fails silently in Safari. I don't have a Mac to test on. I write some stuff and send it to the client who tests and reports back.
I don't do any browser sniffing, I test for a feature for I try to use it...
Are there any known oddities about scripting for Safari that might possibly help me out.
I have a small Javascript problem with that mutch love web browser safari, I tested the code on all other browsers PC (Win) and Linux and IE on the mac and it seams to work ok, but for some reason it will not work with safari.
In my applications I've a ton of scripts that use remote XML file to fill forms and evaluate contents; In these scripts I always use the method SelectNode (that, with some workaround, works fine also in Mozilla).
I've just found out that this method doesnt work in Safari browser, therefore my applications are not usable by this browser. Can anyone provide me any solution or workaround to be able to read XML files in Safari wihout rewriting all of my scripts?