spirit posteets tagged javascript

 

  • 7 years ago
    When the location you want is in the center of the map, copy and paste this code into the location bar of your browser and press enter
    1. javascript:void(prompt(,gApplication.getMap().getCenter()));
  • 7 years ago
    This little trick allows a Flash movie (swf) to find out the URL of the page in which it’s embedded. Javascript must be enabled for this to work. if the window.location.href.toString method doesn’t return a value then ExternalInterface.call will return null. Same thing happens when Javascript is disabled.
    1. // This little trick allows a Flash movie (swf) to find out the URL of the page in which it’s embedded.
    2. import flash.external.ExternalInterface;
    3. var pageURL:String =
    4.         ExternalInterface.call(‘window.location.href.toString’);
  • 7 years ago
    if toFixed is not defined
    1. if (!num.toFixed)
    2. {
    3.  Number.prototype.toFixed = function(precision) {
    4.      var power = Math.pow(10, precision || 0);
    5.      return String(Math.round(this * power)/power);
    6.  }
    7. }
    8. // OR
    9.   Number.prototype.toFixed = function(precision)
    10.   {
    11.      var num = (Math.round(this*Math.pow(10,precision))).toString();
    12.      return num.substring(0,num.length-precision) + “.” +
    13.             num.substring(num.length-precision, num.length);
    14.   }
  • 7 years ago
    1. anchor = window.location.hash;
    2. anchor = anchor.substring(1,anchor.length); // enleve le #
    3. //– Juste pour l’archivage, voici le code que j’utilisais avant de savoir qu’une fonction js existait
    4. var url = document.location.toString();
    5. if (myFile.match(‘#’)) {
    6.     var anchor = myFile.split(‘#’)[1];
    7. }
  • 7 years ago
    1. // Le JS dans la div ci-dessous a été inséré par une requete AJAX
    2. // <div id=’mydiv’> <script type=’text/javascript’> […] </script> </div>
    3. var div = document.getElementById(‘mydiv’);
    4. div.innerHTML = innerHTML;
    5. var x = div.getElementsByTagName(“script”);
    6. for(var i=0;i<x.length;i++)
    7. {
    8.        eval(x[i].text);
    9. }
  • 8 years ago
    1. Remove the events from the options, use an event directly on the select instead.
    2. function myfunc(value) {
    3.   alert(value);
    4. }
    5. <select onchange=“myfunc(this.options[this.selectedIndex].value)”>
    6.   <option value=“1”>Item1</option>
    7.   <option  value=“2”>Item2</option>
    8. </select>
  • 8 years ago
    1. var myfunc = function(optional) {
    2.   if (typeof optional == “undefined”) {
    3.     optional = “default value”;
    4.   }
    5.   alert(optional);
    6. }
  • 8 years ago
    1. $.create = function() {
    2.   var ret = [], a = arguments, i, e;
    3.   a = a[0].constructor == Array ? a[0] : a;
    4.   for(i=0; i<a.length; i++) {
    5.     if(a[i+1] && a[i+1].constructor == Object) { // item is element if attributes follow
    6.       e = document.createElement(a[i]);
    7.       $(e).attr(a[++i]); // apply attributes
    8.       if (a[i] == ‘table’) alert (‘titi’);
    9.       if(a[i+1] && a[i+1].constructor == Array) $(e).append($.create(a[++i])); // optional children
    10.       ret.push(e);
    11.     } else { // item is just a text node
    12.       ret.push(document.createTextNode(a[i]));
    13.     }
    14.   }
    15.   return ret;
    16. };
    17. // Usage
    18. $.create(
    19.    ‘table’, { ‘class’:“MyTable” }, [
    20.       ‘tbody’, {}, [
    21.          ‘tr’, { ‘class’:“MyTableRow” }, [
    22.             ‘td’, { ‘class’:“MyTableCol1” }, [ “howdy” ],
    23.             ‘td’, { ‘class’:“MyTableCol2” }, [
    24.                “Link:”, ‘a’, { ‘class’:“MyLink”, ‘href’:“http://www.example.com” }, [“example.com”] ] ],
    25.          ‘tr’, { ‘class’:“MyTableRow2” }, [
    26.             ‘td’, { ‘class’:“MyTableCol1” }, [ “howdy” ],
    27.             ‘td’, { ‘class’:“MyTableCol2” }, [
    28.                “Link:”, ‘a’, { ‘class’:“MyLink”, ‘href’:“http://www.example.com” }, [“example.com”] ] ] ] ]);

    “javascript” related tags

    spirit’s tags