JavaScript - Primitive To Object Conversion (workaround?)
I'm currently constructing a javascript "terminal" on a site I'm building so that I can test objects and functions outside of the web developer terminal in google chrome.
Given that I have active javascript defining an object called item, typing item in the chrome terminal returns Object and goes on to list the properties of item. Moreover, I can define a function in the terminal like this: function(x) { return x }, and then function(item) will return Object and list the properties. However, if I use form data and user input in the place of the argument, the datatype becomes a string and not an object, so typing item in the form and clicking a button that performs function(document.form.text.value) returns the string item and not Object. Is there any way to convert this primitive string to an object so that I can then iterate through properties, and so on? Similar TutorialsI'm writing a small browser-side Javascript that parses XML from a file and modifies it using E4X. I need to get the result to an xml text file without making the user go through a dozen hoops. Since making a JS client do file I/O is nearly impossible I gave up on that. My next idea was to make Firefox open a new window of contentType text/xml, with my serialized xml in it: Code: var xmlDoc = new XML(SourceXMLString); //SourceXMLString is read from an xml text file output = window.open(""); output.document.open("text/xml"); output.document.write(xmlDoc.toXMLString()); output.document.close(); However any document.write instance seems to set the contentType to text/html and so all the tags are rendered wrong, naturally. From https://bugzilla.mozilla.org/show_bug.cgi?id=73409 it seems that document.open creates the right contentType, but document.write messes it up. Note: this is for a specific internal project, not the public internet. Therefore it only needs to work in Mozilla 3+, and also server-side is not an option. I'm totally open to other suggestions. What's the most streamlined/easiest way, for the code and the user, to get from that XML construct to an .xml file? Hey Guys, I'm trying to make a popup. Let me rephrase, not the type of popup that we all have cursed upon! But the same concept. I have a "control panel" type web application that i have developed. It is all completely dynamic. Each application has its own index.php file. My problem is, one of my applications i need has to open in a new window since its an external site. My problem is the way i have it setup dynamically in php i MUST have that inital index.php file. So what i have tried is to put in that index file just some javascript that opens a new window. Of course both IE and FF's popup blocker kick in. I need to find away around this. Does anyone have any ideas? So far i'd tried a window.open.... also tried creating a link with a target and id and then using click(); to deploy it. And i even tried creating a fake form with a target and an id and submiting it with javascript. Everything gets blocked. Thanks in advance, Jamie I have 10 buttons on a page, and 10 more "onmouseover buttons" that correlate to the first set. Each button or onmouseover button is only 1-2KB! But I can still hold the mouse over a button for a couple seconds until it loads the onmouseover button. I'm using javascript to have the buttons change. The page does load a 2MB video. Could that be the cause? Can I tell it to load the buttons first somehow? Any ideas?
Hi, I am trying to write a small web app that is used to do some plots for over-the-time-collected data (children's growth measurements). The current objective is to re-use entered data. As I do not want to store anything on the server (which would lead to all the annoying username-password-stuff), nor in cookies, I would like put measurements into a local file and reload them into the HTML entering form. I know and fully appreciate that javascript is not able to read or write files; it is on the other hand obvious that this can be bypassed if I upload the respective file to the server which returns it in an ajax fashion. My main question is now: Is it possible to do some kind of internal upload, that ends up in a read of a local file? Something like a <form method="post" action="localscriptname" ...> with an <input type="file"...> field? Another (off-topic) question: My plots are in PDF format. I accidentally appended some data to the PDF file which did not cause any problems for the pdf readers. Can I rely on the fact that any pdf reader will ignore data after the %%EOF mark, but will keep it if a file is downloaded, directly opened by acroread or whatever, and later saved from the reader program? This would allow it to keep the plot and the raw data in one file and might be helpful for the not-so-computer-affine target audience of my program. Greetings! Hi, every time I try and alert: [ { number:0, secondnumber:0 }, { number:2, secondnumber:1 }, { number:1, secondnumber:2 } ] it just shows [object object], [object object], [object object]. Why is this and what can I do to make the record be shown as it is above in an alert? Thanks. I can't get any info from Firebug except that one line, uncaught exception [object Object]. The code fully worked, then I needed to make it dynamically create Sortables from the scriptaculous library based on how many X were in a table in my database, which I've done, and I'm thinking it may be a simple slight parse error of some type, I'm not too good with Javascript, because now my script barely works. I've double checked the script's source code, the PHP variables are exactly what they should be. Code: print<<<HERE Sortable.create('sortlist$box', { tag: 'img', overlap:'horizontal',constraint:false, containment: $list, dropOnEmpty: true, onChange: function(item) { var list = Sortable.options(item).element; if(changeEffect) changeEffect.cancel(); changeEffect = new Effect.Highlight('changeNotification', {restoreColor:"transparent" }); }, onDrop: function(item) { var thing=Sortable.options(item).element.identify(); var anchors = document.getElementById(thing).childNodes.length-2; if(anchors > 20){ alert('This box had 20 creatures in it already, your last action has not been saved.'); window.location.reload(); } else{ new Ajax.Request("saveImageOrder.php", { method: "post", parameters: { data: Sortable.serialize("sortlist$box") } }); } } }); HERE; $box++; } ?> }); </script> if you solve this I'll send ya $10 via paypal I created a method for displaying an object's properties: Code: renderfunction = false; function showProperty (object, property) { document.write ('<td class="type">' + (typeof object[property]) + '</td>' + '<td class="name">' + property + '</td>'); document.writeln('<td class="value">' + ( (typeof object[property] != 'function') ? object[property] :( (property != 'showProperties') ? ( renderfunction ? object[property]() : ('<span class="self">NOT RENDERED</span>') ) : ('<span class="self">THIS</span>') ) ) + '</td>'); document.writeln('<td class="hasOwnProperty" >' + ( object.hasOwnProperty(property) ? "Local" : "Inherited" ) + '</td>'); if (typeof object[property] == 'function') { document.writeln ('<td class="function">' + object[property] + '</td>'); } else { document.writeln ('<td class="function"> </td>'); } } As long as renderfunction = false, the object is fine coming out of this function. However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to? How should I fix it? Thanks in advance, -Brian. Ignore post (if mod, please delete)
Hello together! I generate html code with jsp. In that jsp there a several framesets and frames. And yes i know, frames are not really up to date but it's an old program and i have to deal with it now. Anyway, in the top frameset i have an onload attribute like onload="load()". In the function load i want to access the Element.prototype object. But unfortunately typeof Element gives me "undefined". So i looked a little deeper and found that window.toString() gives me "[object]" and not as expected "[object window]" so somehow my window doesn't know that its construcor is Window. window.construcor is "undefined" as well. And i don't have access to the Element object. I really don't know where the error could be. When the page is loaded and i access the same window over the console, then everything is right. But in my function a can't get access to the objects i need. I also don't know what part of the code could be useful to post here, but maybe someone had a similar problem before? i should say that this problem only occurs in IE8. In IE9 it works perfectly. Has anyone any idea?? Hi all, I'm stumped on finding a way in javascript to create an object factory whose instances are also object factories. In short I want something like that below, but no joy ... any clues? Code: function createClass () { return new createClass() function createClass() { return new createInstance () function createInstance () { //Default properties, values and methods which might later be extended } } } var createDoor = createClass(); var door1 = createDoor(); var door2 = createDoor(); var createChair = createClass(); var chair1 = createChair (); var chair2 = createChair (); I'm writing a program that involves a network of interconnected nodes (or simply objects in my example below). It depends on being able to access properties of an object's linked objects (a bit oddly worded, sorry)... Problem is I'm not sure how to properly access those properties... see below please. <script> //This is an example of a problem im having in my own code... //I want to access the name of the object within the links array wintin the object... var objA = {name: "Object A", links: [objB, objC]}; var objB = {name: "Object B", links: [objC, objD, objE]}; var objC = {name: "Object C", links: [objB]}; var objD = {name: "Object D", links: [objE]}; var objE = {name: "Object E", links: [objD]}; //ex: I want to access the name of Object A's first link... console.log(objA.links[0].name); </script> I'm hoping to get "Object B"... But instead I get: TypeError: Result of expression 'objA.links[0]' [undefined] is not an object. Is there another way around this? Any thoughts are appreciated. Hi, am new to Java, Jquery and Java Script and I am finding my way through recently... I have this HTML code Code: <html:text styleClass="ui-inputText" size="12" styleId="fromDate" property="fromDate" name="HomePageForm" readonly="readonly" /> <script> initDatePicker("fromDate"); </script> I want to perform this action using JS. This will create a text-box with a calender affixed to it if used in Plain HTML... This is the initdatepicker function Code: function initDatePicker(dateFieldId){ var format = '<%=ctx.getDateFormat()%>'; if (format == "yyyy-MM-dd"){ format= "yy-mm-dd"; }else if (format == "MM/dd/yyyy"){ format= "mm/dd/yy"; }else if (format == "dd-MMM-yyyy"){ format= "dd-M-yy" } $("#"+dateFieldId).datepicker({ showOn: 'button', buttonImage: 'include/images/icons/calendar.png', buttonImageOnly: true, changeMonth: false, changeYear: false, buttonText: 'Select date' , dateFormat: format }); } I have done a few things, but, could not get the result I need... Code: cellIndex++; var cellObj=$('#genericRules tr:eq('+index+') td:eq('+cellIndex+')'); cellObj.html(''); var table = document.getElementById("genericRules"); var rowElem = table.rows[index]; var cell = rowElem.insertCell(3); var txtInp = getTextField(indexName,index,'8'); cell.appendChild(txtInp); var dateImg = getImage(index); cell.appendChild(dateImg); cell.align="center"; initDatePicker(indexName + '['+index+'].'+name+'from'); function getTextField(indexName, index, size) { //, maxlength var txtInp = document.createElement('input'); txtInp.setAttribute('type', 'text'); txtInp.setAttribute('class', 'ui-inputText'); txtInp.setAttribute('indexed', 'true'); txtInp.setAttribute('name', indexName + '['+index+'].'+name+'from'); txtInp.setAttribute('id', indexName + '['+index+'].'+name+'from'); txtInp.setAttribute('size',size); txtInp.className = "ui-inputText"; txtInp.name=indexName+'['+index+'].'+name; txtInp.indexed ='true'; return txtInp; } function getImage(index){ var image = document.createElement("img"); image.src = "<%=request.getContextPath()%>/include/images/icons/calendar.png"; image.setAttribute('class', 'inputCalendar'); image.className = "inputCalendar"; image.name = "imgDatevalue"+index; image.id = "imgDatevalue"+index; return image; } My application is now showing me the image but, am not able to get the calender when i click on it.. I need help on this thing... Please try to provide me with soem intel... Quote: menu: function( a, b ) { $( b ).observe( 'click', function( event ) { event.stop(); if( $( a ).visible() ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } else { $( a ).show(); $( b ).addClassName( 'selected' ); document.observe( 'click', function( e ) { if( e.target.id != a && e.target.id != b && !Element.descendantOf( e.target, $( a ) ) ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } }); } }); $$( '#' + b + ' > a' ).each( function( element ) { element.observe( 'click', function( event ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); }); }); } This work's perfrect accept when i use it with others on the menu it leaves the other ones open, how do i get it to close the open one when i open a new menu.. Thanks. Hello. Is there any way to get the variable name of an object from inside the object? E.g. PHP Code: function Bla(){ this.write = function(){ document.write(<objectname>); } } obj1 = new Bla(); obj1.write(); //Outputs obj1 Here is my script: PHP Code: function myTimer(seconds, obj){ this.seconds = seconds; this.obj = obj; this.startTimer = function(){ if(this.seconds>0){ this.seconds--; this.obj.innerHTML = this.seconds; this.timer = setTimeout("Timer.start()",1000); } } } Timer = new Timer(10, obj); Timer.startTimer(); Now the problem is that the variable that contains the object must be named "Timer". This way I cannot create new timer objects with different variable names I have tried: this.timer = setTimeout("this.start()",1000); but it doesn't work. That's why it would be good to detect the variable name and instead use something like this: this.timer = setTimeout(varname+".start()",1000); I would rather not have to pass the variable name through a parameter like this: Timer1 = new Timer(10, obj, "Timer1"); Thanks in advance. Dear All, On my HTML page, I am trying to use as a snippet the script for automatic currency conversion. It is well known that exchange rates between currency are being changed very often (most of times even daily) and I don't want to republish either page with script or entire website every time the exchange rate changes. For website design I am using xsitepro software because I don't know programming. So what I would like to have on specific page of website is something similar to table (if anyone has any better recommendation than using a table, please share). In this table I would like to state prices of my services. Primary currency is only one. So in this column, the numbers shouldn't be changed unless I want to change the prices (and of course therefore republish the website, no need to republish sitemap). In the rest of the columns the numbers should be changed immediately, without needing to republish anything (thats the point of the script) and also rounded to each 5. What I mean with ''each 5'' is shown on few of the following random chosen examples (1500): 1500.03 is rounded to 1500 1500.30 is rounded to 1500 1500.50 is rounded to 1500 1502.49 is rounded to 1500 1502.50 is rounded to 1505 (!) 1504.99 is rounded to 1505 1505.01 is rounded to 1505 1507.49 is rounded to 1505 1507.50 is rounded to 1510 and so on. Idea is to prevent from getting coins. So the break point is on 2.50, 7.50, 12.50, 17.50, 22.50, 27.50 and so on. Not sure how to mathematically describe this. Hopefully I was understandable. I want to have in the columns only the currencies that I am willing to accept if potential client cannot change the currency to my primary one (given in second most left column) in his local exchange office. But I repeat that what Im trying to do is having automatically and immediately updated numerical values on particular HTML page based on most recent exchange rates (of course trustful source is needed). Since I don't know programming, I did some research on google and discovered this: http://coinmill.com/webmaster_options.html The downer script on the right side looks ok but its not even close to what I need due to five reasons: - it is being converted outside the table - with such way i cannot clearly show which currency is primary (prefered) one - conversion is being done to only one other currency at the same time - no rounding to ''each 5'' - unknown source (and therefore untrustful one) of updating most recent exchange rates Table such as this one: http://www.x-rates.com/ looks great but still not meeting my requirements. I would like to replace flags in the left column with ''service 1'', ''service 2'', ''product 1'' etc. Also I have no idea how rounding could be done. So at the end I would end up with something like (here Im showing only two ''still willing to accept'' currencies just to show an example): Currency:Euro Currency:American Dollar Currency:Japanese Yen Service1 500 670 51535 Service2 400 535 41228 Service3 200 265 20614 Top left corner as it is on x-rates.com would be even better. note: when I previewed my post before submitting, I noticed that the forum doesn't let me make so much spaces but I think those numbers are still understandable where do they belong. Any help would be muuuuuch appricated Hi all, First of all thanks for everyone who responds to me. I appreciate it. Here is my problem. I have a HTML form containing two text box controls in it and a submit button. When a user enters information in those two textboxes and click on submit, the information is sent to a function in Javascript. In the javascript, the information from those textboxes is stored in a javascript variable. The problem is as follows: When I am inputting string text in the html text boxes and in the javascript when I am trying to print those values, it is giving me out an error saying NaN. However when i input integer values in the text boxes it is printing those numbers. Is there a conversion that I have to do for the string to be printed. I am new to Javascript and need your help. This is a basic code of Javascript. Below is the code that I have. Thanks in advance for your help. <html> <script type = "text/javascript"> function square(form) { var test = form.value1.value; window.alert(+test); var point = document.test.value1.value; pointcon = point.toString(); window.alert("nice"); window.alert(+pointcon); var point2 = eval(document.test.value2.value); var point3 = point + point2; alert(point3); } </script> <head> <body> <form name = "test"> First name: <input type="text" name = "value1" value1 = ""/><br /> Last name: <input type="text" name = "value2" value2 = ""/> <input type = "button" value = "Submit" onClick="square(this.form)"/> </form> </body> </html> I would like to compare dates in the format used in twitter, which is this: Wed Apr 08 14:30:10 +0000 2009 How do I do this? Do I need to convert to a timestamp first, and if so, how do I do this? G Hi, Please bear with me as I have zero knowledge of programming, or even if this is the right way to do this. I use a website that has (i believe) Javascript on that uses some maths, and I am looking to convert this webpage into excel... I wonder if someone could give me a pointer to convert the attached code into the excel format, or even just to highlight where the maths is so i can continue myself! Code: script is too long, so ive attached as a zip Hey everyone, I have a slight problem with my program and am in need of some help. I am writing a program that will take user input and convert the 8 digits the user enters (the 0's and 1's) and converts them into an integer between 0 and 255. It is a very simple program but I am still having some errors :/ If anyone could take a look at the program and get back to me, it would be very helpful! Code: import javax.swing.*; public class BinaryToInteger { static String userInput = JOptionPane.showInputDialog("Enter a binary number with 8 integers to convert."); //Dialog Box that asks for user input static int binaryValue = Integer.parseInt(userInput); //Parses the user's input into an integer public static void convert() //Method to convert the user's input into a number { char firstNumber = userInput.charAt(0); //Reads the eight numbers the user enters char secondNumber = userInput.charAt(1); char thirdNumber = userInput.charAt(2); char fourthNumber = userInput.charAt(3); char fifthNumber = userInput.charAt(4); char sixthNumber = userInput.charAt(5); char seventhNumber = userInput.charAt(6); char eigthNumber = userInput.charAt(7); System.out.print(firstNumber); //Just to check to see if the correct numbers are being read from the JOptionPane System.out.print(secondNumber); System.out.print(thirdNumber); System.out.print(fourthNumber); System.out.print(fifthNumber); System.out.print(sixthNumber); System.out.print(seventhNumber); System.out.println(eigthNumber); int finalNumber = 0; //Initializing "finalNumber" if(firstNumber == 1) //Checking to see if "firstNumber" is equal to 1 then add 128 to "finalNumber" if it is, and do nothing if it is not { //The same applies below finalNumber = finalNumber + 128; } else { } if(secondNumber == 1) { finalNumber = finalNumber + 64; } else { } if(thirdNumber == 1) { finalNumber = finalNumber + 32; } else { } if(fourthNumber == 1) { finalNumber = finalNumber + 16; } else { } if(fifthNumber == 1) { finalNumber = finalNumber + 8; } else { } if(sixthNumber == 1) { finalNumber = finalNumber + 4; } else { } if(seventhNumber == 1) { finalNumber = finalNumber + 2; } else { } if(eigthNumber == 1) { finalNumber = finalNumber + 1; } else { } System.out.print(finalNumber); //Just to see what the compiler spit out as the answer } public static void main(String []args) //Main method that runs the convert() method { convert(); } } I am trying to fix a problem that I have with my JavaScript assignment for class. The code I have so far is beneath. I ran FireBug on it, and came back with "missing ) after argument list on Line 24" Code: <SCRIPT LANGUAGE="JavaScript"> function Convert(){ // declare a variable var c var f // get variable's value f = parseFloat(document.getElementById('c').1.8 * c + 32) } </SCRIPT> Code: <FORM NAME="Assignment5"> <B>Instructions: </B> Input the temperature in degrees Centegrade and click "Convert" to convert the temperature from degrees Centegrade to degrees Farenheit.. <BR> <BR> <B>Input Values: </B> <BR> <B>Degrees <I>Celsius</I>:</B> <INPUT ID="c"> <BR><BR> <INPUT TYPE="Button" VALUE="Convert" OnClick="Convert();"> <BR><BR> <B>Output Value: </B> <BR> <B>Degrees <I>Farenheit</I>:</B> <INPUT ID="f"> </FORM> Thanks in advance for the help! |