SitePoint Sponsor

User Tag List

Page 3 of 5 FirstFirst 12345 LastLast
Results 51 to 75 of 112
  1. #51
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use document.bgColor. Use document.body.style.backgroundColor instead.

    These three references are equivalent.
    Code:
    document.body.style.backgroundColor
    document.body.style['backgroundColor']
    document['body']['style']['backgroundColor']
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  2. #52
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know, but that was just an example.

    My question remains: Can there be something like pointers in javascript; is there a way to change the value of a system's variable fromout a plugin, when the name of the system's variable can change...

    I'm writing a plugin for online WYSIWYG editors that needs to be able to address the variable of the editor specifying whether the loaded content had been changed or not. The name of this varibale (or a pointer to it) could be specified in the config.js file of the plugin.
    By just knowing the name, the value can be changed for window properties or global variables like I showed above, but should the variable that my plugin needs to alter be on an object (who knows where the editor has its variables) the method I used fails...

  3. #53
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    global variables in javascript can be accessed as properties of the window object
    Code:
     var myVar = 'some value';
    var varName = 'myVar';
     alert( window[varName] );
     // some value
    I think that's what you're asking for?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #54
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That I know (like I showed in my example script) but how do I do this for Object properties?

    When the editors state is keps on, let's say 'window.editor.contentChanged' can I alter that variable's value in the same manner?

  5. #55
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code:
    window['editor']['contentChanged']
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #56
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooooops sorry guys.
    What I needed was actually really simple after all ...

    config:
    Code:
    systemContentChanged= "editor.changed"

    code:
    Code:
    if (saved) {eval(systemContentChanged + " = false;");}

  7. #57
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that works if you don't mind using eval(), which is really slow.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  8. #58
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bracket notation for member access is preferred over eval. The example Kravvitz provided is what you're after.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #59
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey all. New reader, semi-new coder here.

    I just recently started developing some unobtrusive javascript for a new project. I'm by no means a skilled coder. Mostly, it's quick and dirty and it does what I need. I'm sure this can be streamlined, but I thought I'd post it since it was helpful to me.

    I wanted a way for off-site links on my site to open in a new window. My favorite way to do that was with the blank target attribute but since I was writing a strict XHTML site, this was not an option. My solution was a script which parses the links on the page, searches the rel attribute for the word external and then modifies the target attribute accordingly. I also added the protected keyword for password protected pages. I'm using innerHTML which I know is sinful, but it works for my audience for the time being. Anyway, here's the code:

    HTML Code:
    [code]
    function externalLinks() {
      if (!document.getElementsByTagName) return;
      var anchors = document.getElementsByTagName("a");
      for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external"){
          anchor.target = "_blank";
          anchor.innerHTML = anchor.innerHTML + " &#xBA;";
        }
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "protected"){
          anchor.target = "_blank";
          anchor.innerHTML = anchor.innerHTML + " *";
        }
     }
    }
    [code]
    I'd love to get some feedback or improvement tips if ya've got some.
    Bill Brown,
    Webmaster, MacNimble.com
    Last edited by MacNimble; Apr 20, 2006 at 00:33. Reason: Botched Syntax Highlighting

  10. #60
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe using a base with target attribute can help? But I don't know if that is valid XHTML? http://www.w3schools.com/tags/tag_base.asp and maybe this thread is usefull on this topic: http://forum.echoechoplus.com/showth...t=target+XHTML

  11. #61
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thx for sharing

  12. #62
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    change background color to black:
    void(document.bgColor='#000000');
    change foreground color to white:
    void(document.fgColor='#ffffff');
    this could help
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #63
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Sweden
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Break out of frames-script

    An external "Breakout-script" to prevent forrein websites from capturing your website in a frame.

    Code:
    /*Does the browser support DOM?*/
    ie = (document.all) ? true:false; // IE4+
    dom = ((document.getElementById) && (!ie)) ? true:false; // Mozilla
    /*Call to the help function*/
    setEventByObject(window,"load", breakingOutOfFrames);
    /*Help function*/
    function setEventByObject(ob, ev, fu) {
    if(dom) {
    ob.addEventListener(ev, fu, false);
    }
    if(ie) {
    ob.attachEvent('on' + ev, fu);
    }
    }
    /*Breaks out of frames*/
    function breakingOutOfFrames() {
    if (top.location != location) {
    top.location.href = document.location.href;
    }
    }

  14. #64
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Sweden
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to make a Bookmark this site button

    "Bookmark this site" Button to be put within the head tags
    of your HTML document, or even better, put it in an external file(without script tags) and link to that file in the head:


    Code:
    <script language="JavaScript" type="Text/Javascript">
    <script language="JavaScript" type="Text/Javascript">
    <!-- // 
    /*Avgör läsaren*/
    var ie = (document.all) ? true:false; // IE4+
    var dom = ((document.getElementById) && (!ie)) ? true:false; // Mozilla 
    var url = "http://www.webpelican.com/internet-programming-3/"; 
    var pageName = "Client side programming"; 
    function bookmark() { 
    if (ie) 
    window.external.AddFavorite(url, pageName) 
    else if(dom)  
    window.sidebar.addPanel(pageName, url,""); 
    else {
    alert("Sorry! Your browser doesn't support function. Use the bookmark option in your browser instead."); 
    return true;
    }
    } // --></script>
    Put this somewhere in the body section of your HTML document:
    Button:
    Code:
    <input type="button" value="Bookmark" name="Bookmark" onclick="bookmark()" />
    or

    Simple link:
    Code:
    <a 
    href="javascript:bookmark()">Bookmark
    </a>
    (and of course change the URL to whatever URL you would like to bookmark )

  15. #65
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the details! they're very useful!

  16. #66
    SitePoint Member
    Join Date
    Oct 2006
    Location
    Harrisburg, PA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Javascript Math

    javascript newbie needs help!

    Writing a simple script (yeah right):

    Have the following:
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    <!--
    var radzvalue=0

    function setrad(num){
    radzvalue = num;
    }
    function button1_onclick() {
    var answer, radiovalue=0;
    answer=text1.value;

    if (checkbox1.checked){
    answer=num1-num2;
    }else if (checkbox2.checked){
    anser=num1*num2;
    }else{
    answer=num1/num2
    }
    if (radiox[0].checked)radiovalue=1;
    else if (radiox[1].checked)radiovalue=2;
    else if (radiox[2].checked)radiovalue=3;
    }


    </head>
    <body>
    <p><span id="num1">10</span>
    <p><span id="num2">20</span>
    <span>
    <input id=radiox type=radio name=radiox value=1 />Subtract
    <input id="Radio2" type="radio" />Multiply
    <input id="Radio3" type="radio" />Divide</span></p>

    <input id="Text1" style="width: 49px" type="text" />
    </body>
    </html>


    What should happen is when a number(answer) is entered into the textbox, the script should check to see of the answer is correct. i.e. the default is subtract so 10-20 = -10 etc. Then, random numbers need to be generated to take the place of the default num1 & num2 and the process starts again.

    Any help or guidance would be appreciated.

    Oak

  17. #67
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    woohoo, DateAdd for javascript

    usage:
    Code:
    var d = new Date();
    
    d.addToDate("week",3,false); // adds 3 weeks to date, not time accurate
    alert(d);
    d.addToDate("minute",650); // adds 650 minutes to date, time accurate
    alert(d);
    d.addToDate("quarter",-6,false); // subtracts 6 quarters from date, not time accurate
    alert(d);
    d.addToDate("ms",15000); // adds 15000 milliseconds to date, time accurate
    alert(d);
    
    etc

    Code:
    Date.prototype.addToDate = function () {
    /*
    similar to the DateAdd function in VBS
    
    accepts up to 2 parameters
    
    param1 (unit) <string> optional, default="day" 
    	the type of unit to add to the date
    	can be any of
    	"year","quarter","month","week","day","hour","minute","second","ms" (millisecond)
    
    param2 (qty) <integer> optional, default=1 
    	the number of units to add to the date
    
    param3 (useTime) <boolean> optional, default=true 
    	set this to false if you are only doing date calculations, otherwise you may run into savings time errors!
    	leave it set to true if you are doing time-accurate calculations
    */
    	
    	// default params
    	var unit = "day";
    	var qty = 1;
    	var useTime = true;
    	
    	// get the params if they exist
    	if (arguments.length > 2) {
    		useTime = arguments[2];
    	}
    	if (arguments.length > 1) {
    		qty = arguments[1];
    	}
    	if (arguments.length > 0) {
    		unit = arguments[0];
    	}
    	
    	// allow accidental use of "seconds" eg instead of "second"
    	if (unit.charAt(unit.length - 1) == "s" && unit != "ms") {
    		unit = unit.substr(0,unit.length -1);
    	}
    	
    	// make sure we have a valid qty
    	if (parseInt(qty) != qty) {
    		alert("Bad quantity parameter: " + qty + ", date unchanged");
    	} else {
    		qty = parseInt(qty);
    		
    		// if time-accurate calculations are not used, set the time to 1:00:00AM so we don't have daylight savings errors
    		if (useTime) {
    			var dt = Date.UTC(this.getUTCFullYear(), this.getUTCMonth(), this.getUTCDate(), this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds(), this.getUTCMilliseconds());
    		} else {
    			// add getTimezoneOffset() minutes to 1:00 to get 1:00 =)
    			var dt = Date.UTC(this.getUTCFullYear(), this.getUTCMonth(), this.getUTCDate(), 1, this.getTimezoneOffset(), 0, 0);
    		}
    		var dat = new Date(dt);
    
    		// do stuff depending on which unit was used
    		switch (unit.toLowerCase()) {
    		case "year":
    			// easy, add the quantity to years
    			dt = Date.UTC(dat.getYear() + qty, dat.getMonth(), dat.getDate(), dat.getHours(), dat.getMinutes(), dat.getSeconds(), dat.getMilliseconds());
    			break;
    		case "quarter":
    			// more complicated, calculate months and years to add
    			qty = qty * 3;
    
    			var yearsToAdd = parseInt(qty/12);
    			var monthsToAdd = qty - yearsToAdd * 12;
    			
    			if (dat.getUTCMonth() + monthsToAdd > 11) {
    				yearsToAdd += 1;
    				monthsToAdd -= 12;
    			} else if (dat.getUTCMonth() + monthsToAdd < 0) {
    				yearsToAdd -= 1;
    				monthsToAdd += 12;
    			}
    
    			dt = Date.UTC(dat.getUTCFullYear() + yearsToAdd, dat.getUTCMonth() + monthsToAdd, dat.getUTCDate(), dat.getUTCHours(), dat.getUTCMinutes(), dat.getUTCSeconds(), dat.getUTCMilliseconds());
    			break;
    		case "month":
    			// more complicated, calculate months and years to add
    			var yearsToAdd = parseInt(qty/12);
    			var monthsToAdd = qty - yearsToAdd * 12;
    			
    			if (dat.getUTCMonth() + monthsToAdd > 11) {
    				yearsToAdd += 1;
    				monthsToAdd -= 12;
    			} else if (dat.getUTCMonth() + monthsToAdd < 0) {
    				yearsToAdd -= 1;
    				monthsToAdd += 12;
    			}
    			
    			dt = Date.UTC(dat.getUTCFullYear() + yearsToAdd, dat.getUTCMonth() + monthsToAdd, dat.getUTCDate(), dat.getUTCHours(), dat.getUTCMinutes(), dat.getUTCSeconds(), dat.getUTCMilliseconds());
    			break;
    		case "week":
    			// the rest just add a number of milliseconds since eg 1 week = 1 week always and forever
    			dt = dat.valueOf() + qty * 1000 * 60 * 60 * 24 * 7;
    			break;
    		case "day":
    			dt = dat.valueOf() + qty * 1000 * 60 * 60 * 24;
    			break;
    		case "hour":
    			dt = dat.valueOf() + qty * 1000 * 60 * 60;
    			break;
    		case "minute":
    			dt = dat.valueOf() + qty * 1000 * 60;
    			break;
    		case "second":
    			dt = dat.valueOf() + qty * 1000;
    			break;
    		case "ms":
    			dt = dat.valueOf() + qty;
    			break;
    		default:
    			alert("bad unit parameter: " + unit + ", date unchanged");
    			break;
    		}
    
    		// would have been nice to do this part some other how but I couldn't figure it out
    		dat = new Date(dt);
    		
    		this.setUTCMilliseconds(dat.getUTCMilliseconds());
    		this.setUTCSeconds(dat.getUTCSeconds());
    		this.setUTCMinutes(dat.getUTCMinutes());
    		this.setUTCHours(dat.getUTCHours());
    		this.setUTCDate(dat.getUTCDate());
    		this.setUTCMonth(dat.getUTCMonth());
    		this.setUTCFullYear(dat.getUTCFullYear());
    		
    	}
    }
    Last edited by jimfraser; Dec 7, 2006 at 10:46. Reason: Added a new parameter to avoid some savings time issues

  18. #68
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tip: Comment blocks for troubleshooting

    Sometimes, I find myself with a "test function" and the "real function" residing within the same function/object. When I want to run the test, I comment out the non-test and vice versa. The trouble is, I hate having to type the /* and */ over and over again to do my tests.

    So, I've discovered a really cool way to handle this (borrowed from PHP). You can override /* and */ using line comments // to comment/uncomment your code:
    Code:
    function testFunction() {
      alert("test function");
    }
    function realFunction() {
      alert("real function");
    }
    In that block, I want to switch back and forth between my test and real functions regularly while testing, so I write my comment blocks a special way.

    In this example, the real function is commented out, but the test function is exposed.
    Code:
    //*
    function testFunction() {
      alert("test function");
    }
    /*/
    function realFunction() {
      alert("real function");
    }
    // */
    In this second example, by removing a single '/' above "test function", I'm able to comment out the test function block while at the same time exposing the real function.
    Code:
    /*
    function testFunction() {
      alert("test function");
    }
    /*/
    function realFunction() {
      alert("real function");
    }
    // */
    You can see by the use of creative commenting, you can quickly comment/un-comment blocks of code by only adding or removing a single slash '/' character and not having to worry about any lingering */ that will cause your code to choke.

  19. #69
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnx for the detail.They are really useful

  20. #70
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Excellent thread buddies.
    My little share for trivial tasks (ie el.offsetWidth).
    I think it is quicker than parseInt.
    function getInt(v){
    return +v;
    }
    var v1= 'test1';
    var v2= '300';
    var v3= 300;
    alert(getInt(v1));
    alert(getInt(v2));
    alert(getInt(v3));

    Best wishes

  21. #71
    SitePoint Member
    Join Date
    Sep 2007
    Location
    South China
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a relative newbie to this forum and I would just like to add my thanks to the contributors to this thread. Good stuff!!

  22. #72
    Non-Member MYOWNDollar's Avatar
    Join Date
    Apr 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for good information!!

  23. #73
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prototype referencing, switching and updating

    A discussion of how prototypes work inside and outside of constructor functions.
    http://www.room51.co.uk/js/prototypePlacement.html

  24. #74
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very nice thread... i learn some about javascript with this thread... Thanks for all...

  25. #75
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very useful thread. I have been looking for some of these.


Bookmarks

Posting Permissions

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