JavaScript es6 有引入外部文件的功能 import
import 後得到一個錯誤
Uncaught SyntaxError: Cannot use import statement outside a module
解決方法
必須要宣告為js的模組,在引入js的script tag中加入type=”module” 這個屬性如下
[……]
JavaScript es6 有引入外部文件的功能 import
import 後得到一個錯誤
Uncaught SyntaxError: Cannot use import statement outside a module
解決方法
必須要宣告為js的模組,在引入js的script tag中加入type=”module” 這個屬性如下
[……]
javascript 卷軸自動捲到某一元素
$('html, body').animate({
scrollTop: ele.offset().top - 110 + 'px'
}, 1500 ,'easeInOutQuint');[……]
在PHP中或某些語言中,可以在迴圈中使用 continue 及 break 來進行更靈活的迴圈
在 jQuery 中迴圈是用 $.each(),但使用 continue 及 break 是不起作用的,可改用下列替代方式,可以達成相同目的。
$.each(data, function(index, obj){
if ($(img).empty()) {
return; // 等於 continue
} else {
return false; // 等於 break
}
});
[……]
HTML提供了一個錨點連結的功能 (anchor tag link),換頁或不換頁,都可以把瀏覽器卷軸自動捲到錨點位置,但如果網頁上方有一個黏頭的Header (sticky header),RWD手機版本則習慣使用(sticky header),Header 懸浮本文之上,可能會把錨點的元素內容遮蔽,這時我們則需要偏移(sticky header)的高度,避免錨點元素被遮蔽。[……]
有時需要判斷瀏覽器安裝那些第三方插件,比如:VLC、Flash Player、PDF Viewer等等,藉以作後續的動作,比如說沒有Flash Player引導user安裝,下面這程式就非常有用:
會返回一個已安裝plugin的數組
<!doctype html>
<html>
<body>
<div id="example"></div>
<script type="text/javascript">
var x=navigator.plugins.length; // store the total no of plugin stored
var txt="Total plugin installed: "+x+"<br/>";
txt+="Available plugins are->"+"<br/>";
for(var i=0;i<x;i++)
{
txt+=navigator.plugins[i].name + "<br/>";
}
document.getElementById("example").innerHTML=txt;
</script>
</body>
</html>
輸出:
Total plugin installed: 4 Available plugins are-> Widevine Content Decryption Module Native Client Chrome PDF Viewer
<scrip>
function getFlashVersion() {
var flash = navigator.plugins.namedItem('Shockwave Flash');
if (typeof flash != 'object') {
// flash is not present
return undefined;
}
if(flash.version){
return flash.version;
} else {
//No version property (e.g. in Chrome)
return flash.description.replace(/Shockwave Flash /,"");
}
}
</script>[……]
本篇使用 Javascript , 若想用jQuery的方法,可參考本站另一篇
本篇提供兩種方法,擇一使用
/*
VECTOR COOL
https://vector.cool
*/
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};/*
VECTOR COOL
https://vector.cool
*/
function getURLParameter(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
/*
VECTOR COOL
https://vector.cool
*/
var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');[……]
Drag and drop so simple it hurts
https://github.com/bevacqua/dragula
angular-drag-and-drop-lists
http://marceljuenemann.github.io/angular-drag-and-drop-lists/
GRIDSTACK.JS
http://troolee.github.io/gridstack.js/[……]
設計WordPress主題或插件開發時,常會需要使用jQuery,WordPress其實很貼心的已經載入jQuery,但實際使用時發現,原本可以跑的jQuery程式,放進WordPress卻不能跑了,才知道原來WordPress的jQuery要這樣寫。
<script type='text/javascript' src='https://yourdomain.com/wp-includes/js/jquery/jquery.js?ver=1.11.1'></script> <script type='text/javascript' src='https://yourdomain.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<script>
$( document ).ready(function() {
alert("hello");
});
</script>理論上應該是會順利看到 hello 對話框,但是沒有
主要是jQuery寫法的問題,選擇器不能使用習慣的簡碼 $ 符號,而要用下面這兩種寫法,
順利看到hello 對話框
jQuery( document ).ready( function( $ ) {
alert("hello");
} );( function( $ ){
alert("hello");
} )( jQuery );[……]
// ‘http://127.0.0.1/osac.com.tw/zh-tw/tag_page.php?aaa=1&bbb=2’
// 當前頁面的 URL
window.location.href
// output : ‘http://127.0.0.1/osac.com.tw/zh-tw/tag_page.php?aaa=1&bbb=2’
// 域名
window.location.hostname;
// output : ‘127.0.0.1’
//當前頁的路徑和文件名
window.location.pathname;
// output : ‘/osac.com.tw/zh-tw/tag_page.php’
//所使用的網絡協議(http://或https://)
console.log(‘location.protocol=’+window.location.protocol);
// output : ‘http:’
//取URL參數
urlParam = decodeURIComponent(location.search)
// output : ‘?aaa=1&bbb=2′[……]