Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #app.mx-auto.max-w-2xl.tracking-wider
	h1.py-4.font-semibold.text-center.text-3xl 改變 iframe src 時不增加瀏覽歷史紀錄
	.mb-6.text-center
		p 筆記文:
			a(class="text-blue-400 hover:text-blue-500 hover:underline",
				href="https://www.letswrite.tw/iframe-src-history/",
				target="_blank") Let's Write

	section.mb-6.py-8.bg-white.bg-opacity-10.rounded-xl
		.flex.justify-around
			button#usually.btn.btn-outline.tracking-wider(type="button") 改變 iframe src - 會增加 history
			button#better.btn.btn-outline.tracking-wider(type="button") 改變 iframe src - 不會增加 history

	section.flex
		div(class="w-1/2")
			p.mb-4 iframe:
			#iframe-wrap
				iframe#iframe(width="336", height="189",
							src="https://www.youtube.com/embed/V0XUd8f2pz8",
							allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture", allowfullscreen)

		.flex.flex-wrap.pl-4(class="w-1/2")
			p.mb-4.w-full History Length:
			p#historyLength.w-full.font-black.text-center.text-9xl

              
            
!

CSS

              
                
              
            
!

JS

              
                const iframeSrcList = [
	'https://www.youtube.com/embed/V0XUd8f2pz8',
	'https://www.youtube.com/embed/Vno_QG_LENE'
];

const iframe = document.getElementById('iframe');
let iframeSrc = '';
let iframeSrcIndex = true;

// 改變 iframe src
function checkIframeSrc() {
	iframeSrcIndex ? iframeSrc = iframeSrcList[1] : iframeSrc = iframeSrcList[0];
	iframeSrcIndex = !iframeSrcIndex;
}

// 改變 iframe src - 會增加 history
function changeIframe() {
	checkIframeSrc();
	iframe.src = iframeSrc;
	setTimeout(calHistoryLength, 400);
}

// 改變 iframe src - 不會增加 history
function changeIframeBetter() {
	checkIframeSrc();
	const iframeWrap = document.getElementById('iframe-wrap');
	let iframe = `<iframe width="336" height="189" src="${iframeSrc}" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="allowfullscreen"></iframe>`;
	iframeWrap.innerHTML = iframe;
	setTimeout(calHistoryLength, 400);
}

// 計算 history length
const historyLengthEl = document.getElementById('historyLength');
function calHistoryLength() {
	historyLengthEl.textContent = history.length;
}

// 點擊會增加的按鈕
const btnUsually = document.getElementById('usually');
btnUsually.addEventListener('click', changeIframe, false);

// 點擊不會增加的按鈕
const btnBetter = document.getElementById('better');
btnBetter.addEventListener('click', changeIframeBetter, false);

// 先執行一次計算 history length
calHistoryLength();
              
            
!
999px

Console