2016-06-01 5 views
2

私はwordpressを使用してGoogleマップを統合するウェブサイトを構築しようとしています。私はマップといくつかのオーバーレイをやって、適切なJavaScriptを作るためにGoogleの開発者APIとPythonを使用しています。私はこれを達成するのに必要なjsファイルとPythonを正常に書いています。オンクリックで有効になるワードプレスページのヘッダーにjsファイルを含めるには

私のウェブサイトはWorpressで構築されており、nのリンクを持つページ(ホームページではない)を追加したいと思います。それぞれに対応するマップが表示されます。私は、レイアウトやデザインの問題の世話をすることができますが、私はどのように上の損失で午前:

A)

B、そのファイルとJavaScriptを含める)のリンクをクリックすると呼ばれるので、そのマップを移入します新しいページを呼び出さずに

つまり、数千の緯度/経度の点が含まれる可能性があるため、JavaScriptは大変です。したがって、nをヘッダに書き込むことは不合理です。リンクをクリックすると、filename.jsから電話したいと思います。

ヘッダーに必要なものを含めることができるプラグインがあります。だから、* .jsファイル(またはtxtファイル)をディレクトリツリーにどこに置くかを知ることができ、クリックすると対応するファイルをどのようにアクティブにするかを知ることができればいいはずです。ありがとう!

このDisplay different maps with onClick event - Google Maps V3.は、クリックして表示するのに役立ちますが、誰もが1つのマップを作成することができました。私はそんなことはできません。私は膨大な量のデータを重ねています。

答えて

1

これはあなたがそれを行うことができる方法です。

簡潔にするために、私は1つの 'ファイル'にスクリプトの束を含めましたが、あなたはそれらを個々のファイルに分割したいと思うでしょう。

jsbin js bin exampleでhtmlとjsを試してみる必要があるかもしれませんが、b/c SOはjsの動的ローディングを許可する場合と許可しない場合があります。

(function(undefined) { 
 
    /** 
 
    * @author (@colecmc) 
 
    * @method turn collection into an array 
 
    * @param {object} collection - NodeList, HTMLCollection, etc. Should have an "item" method and/or a "length" property 
 
    */ 
 
    ToArray = collection => Array.prototype.slice.call(collection); 
 

 
    /** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ **/ 
 

 

 
    Observer = (function(undefined) { 
 
    /** 
 
    * pub sub 
 
    */ 
 
    'use strict'; 
 

 
    var subUid = -1; 
 
    return { 
 
     topics: {}, 
 

 
     subscribe: function(topic, func) { 
 
     /** 
 
     * @param {string} topic 
 
     * @param {function} func 
 
     * @returns {string} - a token such as '3' 
 
     * @example Observer.subscribe('any-valid-string',function(name,resp){ 
 
       console.log(resp.prop); 
 
      }); 
 
     */ 
 
     if (!Observer.topics[topic]) { 
 
      Observer.topics[topic] = []; 
 
     } 
 

 
     var token = (++subUid).toString(); 
 
     Observer.topics[topic].push({ 
 
      token: token, 
 
      func: func 
 
     }); 
 

 
     return token; 
 
     }, 
 

 
     publish: function publish(topic, args) { 
 
     /** 
 
     * @param {string} topic 
 
     * @param {object} args 
 
     * @returns {boolean} - true if topic is valid, false otherwise 
 
     * @example Observer.publish('any-valid-string',{ 
 
       prop: 'this is a test' 
 
      }); 
 
     */ 
 
     if (!Observer.topics[topic]) { 
 
      return false; 
 
     } 
 

 
     setTimeout(function() { 
 
      var subscribers = Observer.topics[topic], 
 
      len = subscribers ? subscribers.length : 0; 
 

 
      while (len--) { 
 
      subscribers[len].func(topic, args); 
 
      } 
 
     }, 0); 
 

 
     return true; 
 
     }, 
 

 
     unsubscribe: function unsubscribe(token) { 
 
     /** 
 
     * @param {string} token - value should be saved from the original subscription 
 
     * @example Observer.unsubscribe('2'); 
 
     * @example Observer.unsubscribe(member); - where member is the value returned by Observer.subscribe(); 
 
     */ 
 
     var m, 
 
      forEachTopic = function(i) { 
 
      if (Observer.topics[m][i].token === token) { 
 
       Observer.topics[m].splice(i, 1); 
 
       return token; 
 
      } 
 
      }; 
 

 
     for (m in Observer.topics) { 
 
      if (Observer.topics.hasOwnProperty(m)) { 
 
      Observer.topics[m].forEach(forEachTopic); 
 
      } 
 
     } 
 

 
     return false; 
 
     } 
 
    }; 
 
    }(undefined)); 
 
    /** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ **/ 
 

 
    SetAttributes = function(el, attrs) { 
 
    /** 
 
    * @author (@colecmc) 
 
    * @method simple for in loop to help with creating elements programmatically 
 
    * @param {object} el - HTMLElement attributes are getting added to 
 
    * @param {object} attrs - object literal with key/values for desired attributes 
 
    * @example SetAttributes(info,{ 
 
    * 'id' : 'utswFormInfo' 
 
    * 'class' : 'my-class-name' 
 
    * }); 
 
    */ 
 

 
    'use strict'; 
 
    var key; 
 

 
    for (key in attrs) { 
 
     if (attrs.hasOwnProperty(key)) { 
 
     el.setAttribute(key, attrs[key]); 
 
     } 
 
    } 
 

 
    return el; 
 
    }; 
 

 
    /** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ **/ 
 

 

 
    GetScript = function(url, fullPath) { 
 
    /** 
 
    * @author (@colecmc) 
 
    * @version 1.0.4 
 
    * @requires Swlxws.SetAttributes, Swlxws.Observer 
 
    * @method dynamically add script tags to the page. 
 
    * @param {string} url - relative path and file name - do not include extension 
 
    * @param {string} fullPath - absolute path 
 
    * @example GetScript('myLocalScript'); 
 
    * @example GetScript('','https://www.google-analytics.com/analytics.js'); 
 
    */ 
 

 
    'use strict'; 
 

 
    function onLoad(event) { 
 
     var result; 
 

 
     if (event.type === 'load') { 
 
     result = 1; 
 
     } else { 
 
     result = -1; 
 
     } 
 

 
     Observer.publish('get-script-onload-complete', { 
 
     successful: result, 
 
     eventData: event 
 
     }); 
 
    } 
 

 
    var JSPATH = '/js/', 
 
     /* or where ever you keep js files */ 
 
     el = document.createElement('script'), 
 
     attrs = { 
 
     defer: true, 
 
     src: null, 
 
     type: 'text/javascript' 
 
     }; 
 

 
    /** look for a string based, protocol agnostic, js file url */ 
 
    if (typeof fullPath === 'string' && fullPath.indexOf('http') === 0) { 
 
     attrs.src = fullPath; 
 
    } 
 

 
    /** look for any string with at least 1 character and prefix our root js dir, then append extension */ 
 
    if (typeof url === 'string' && url.length >= 1) { 
 
     attrs.src = JSPATH + url + '.js'; 
 
    } 
 

 
    SetAttributes(el, attrs); 
 

 
    el.addEventListener('load', onLoad); 
 
    el.addEventListener('error', onLoad); 
 

 
    document.body.appendChild(el); 
 

 
    return el; 
 
    }; 
 

 
    /** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ **/ 
 

 
    /** 
 
    * Get Started 
 
    */ 
 
    function onClick(event) { 
 
    GetScript('', event.target.dataset.namespaceUrl); 
 
    } 
 

 
    Observer.subscribe('get-script-onload-complete', function(name, resp) { 
 
    /** check to make resp is what you expect, ie: the correct script loaded */ 
 
    /** then it is safe to use */ 
 
    }); 
 

 
    ToArray(document.querySelectorAll('.load-scripts')).map(script => script.addEventListener('click', onClick, false)); 
 

 
}(undefined));
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>How to include js files in header of wordpress pages that are activated on-click</title> 
 
</head> 
 

 
<body> 
 

 
    <a href="#load" class="load-scripts" data-namespace-url="https://www.google-analytics.com/analytics.js">Load Google Analytics</a> 
 

 
</body> 
 

 
</html>

0

あなたがしたいだけのテンプレートに必要なJSファイルをロードする機能wp_enqueue_script()を使用することができます。

大きなデータセットについては、外部の.jsonファイルにキャッシュし、必要な場合にのみwp_enqueue_script()を使用してロードすることをお勧めします。

0

もし、onclickイベントの提案があなたの望むものであれば、大量のデータが心配です。それに取り組むにはいくつかの方法があります。私は、データセットがjsファイルかphp/jsonファイルかどうかはわかりませんが、私のプロジェクトの1つで同様の問題が発生しました。正しくは覚えていませんが、maxmindのip/locationデータセットで何かしていました。

私は大きなファイルを3つの小さなファイルに分割しました。その後、私はそれぞれのファイルをループし、探していたものがファイル内に見つかった場合、私はちょうど壊れました。そして、ブライアンがキャッシュの提案をしてCDNを使うことは、大いに役立つはずです。

関連する問題