www.webdeveloper.com
Results 1 to 10 of 10

Thread: Problem with Javascript.

  1. #1
    Join Date
    Jan 2006
    Posts
    64

    Problem with Javascript.

    Code:
    var button = false;
    var B = "<button type='button' script = 'var button = true'>Calculate!</button>" ;
    var n = 0;
    var y = new Array();
    var x = /([1-10])\/10/g ;
    var tds = document.getElementsByTagName("td");
    var titles = document.getElementsByTagName("h2");
    
    for(var j = 0; j < titles.length; j++) {
    var title = titles[j];
    	title.innerHTML = title.appendChild(B); //Problem is right here.
    }
    
    if (button = true){
    for(var i = 0; i< tds.length; i++) {
     var td= tds[i];
    	if(td.innerHTML == x) 
    	{
    		x = y[i];
    	}
    	for(var z = 0; z < i; z++) 
    	{
    	n = n + y[z];
    	}
    	
    	window.open(document.write("<p>" + n + "</p>"));			
    	}
    									
    }
    
    button = false;
    The problem is where the comment specifies it to be. For some reason Greasemonkey won't let me run it.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    I don't think that Greasemonkey understands HTMLELement.innerHTML and you need to use the DOM.

  3. #3
    Join Date
    Jan 2006
    Posts
    64
    New Code

    Code:
    var button = false;
    var B = document.createElement("button");
    var n = 0;
    var y = new Array();
    var x = /([1-10])\/10/g ;
    var tds = document.getElementsByTagName("td");
    var title = document.getElementsByTagName("h2")[0];
    
    B.setAttribute('type', 'button');
    B.setAttribute('value', 'var button = true');
    B.setAttribute('onclick', 'var button= true')
    title.appendChild(B);
    
    if (button = true){
    for(var i = 0; i< tds.length; i++) {
     var td= tds[i];
    	if(td.innerHTML == x) 
    	{
    		x = y[i];
    	}
    	for(var z = 0; z < i; z++) 
    	{
    	n = n + y[z];
    	}
    	
    	window.open(document.write("<p>" + n + "</p>"));	//Security Error		
    	}
    									
    }
    
    button = false;
    Error Console is telling me this is a security error. What can be done to fix this?

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    I think that you want:
    Code:
    window.open ('', 'win').document.write ('<p>', n, '<p>');
    
    // or if you're not into the whole brevity thing:
    
    win = window.open ('', 'win');
    win.document.write ('<p>', n, '<p>');
    Last edited by Charles; 05-22-2009 at 12:45 PM.

  5. #5
    Join Date
    Jan 2006
    Posts
    64
    Well, that got the window to show up. However it's still telling me it's a security error. :/

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    I had a "cut and paste" error up there. Give it another go and if it still isn't working post what you then have.

  7. #7
    Join Date
    Jan 2006
    Posts
    64
    I got to leave for work soon so I'll just post what I have so far.

    Code:
    var button = false;
    var B = document.createElement("button");
    var n = 0;
    var y = new Array();
    var x = /([1-10])\/10/g ;
    var tds = document.getElementsByTagName("td");
    var title = document.getElementsByTagName("h2")[0];
    
    B.setAttribute('type', 'button');
    B.setAttribute('value', 'Calculate!');
    B.setAttribute('onclick', 'button= true');
    //t = B.firstChild.document.createElement("p");
    //t.nodeValue = "Calculate!" 
    title.appendChild(B);
    
    if (button = true){
    for(var i = 0; i< tds.length; i++) {
     var td= tds[i];
    	if(td.innerHTML == x) 
    	{
    		parseFloat('x');
    		x = y[i];
    	}
    	for(var z = 0; z < i; z++) 
    	{
    	n = n + y[z];
    	}
    	
    			
    	}
    	window.open('', 'win', height = 300, width = 300).document.write('<p>' + (n/z) + '</p>');	//Security Error.								
    }
    
    button = false;

  8. #8
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    Try
    Code:
    window.open('', 'win', 'height=300,width=300').document.write('<p>', (n/z), '</p>');

  9. #9
    Join Date
    Jan 2006
    Posts
    64
    I'm STILL getting the security error for some unknown reason. :/

  10. #10
    Join Date
    Jan 2006
    Posts
    64
    Well, whatever. Everything else works, syntax wise. However I still have one nagging problem. I wanted this program to cycle through <td> elements and scoop up everything in a "n/10" format and calculate the average mean. For some reason it's not displaying the result. I'll post what I have so far.

    Code:
    var button = false;
    var B = document.createElement("input");
    var n = 0;
    var y = new Array();
    var x = /([0-9]{2})\/10/g ;
    var c = 0;
    var tds = document.getElementsByTagName("td");
    var title = document.getElementsByTagName("h2")[0];
    
    
    B.setAttribute('type', 'button');
    B.setAttribute('value', 'Calculate!');
    B.setAttribute('onclick', 'button= true');
    title.appendChild(B);
    
    if (button = true){
    for(var i = 0; i< tds.length; i++) {
     var td= tds[i];
    	if(td.innerHTML == x) 
    	{
    		parseFloat('x');
    		if(x.NaN = true){
    		i++;
    		c++;
    		}
    		if(x < 1){
    		i++;
    		c++;
    		}
    		if(x > 10){
    		i++;
    		c++;
    		}
    		x = y[i];
    		n = n + y[i];
    	}
    	//for(var z = 0; z < i; z++) 
    	//{
    	//n = n + y[z];
    	//}
    	
    			
    	}
    	window.open('', 'win', 'height=300,width=300').document.write('<p>', (n/(i-c)), '</p>');								
    }
    
    button = false;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles