简体   繁体   中英

Embed doesnt show up when clicking a link to go to the page with the embedded content

I'm experiencing one of the strangest web bugs I've seen right now and its kind of hard to explain but I'll try my best to.

I'm hosting a small flash games website and I have run into a bug with the way that I'm embedding games. Because I really don't feel like writing an HTML file for each game to be embedded on, I created a webpage that when you provide a "URL variable" with the file name of the game, it will create an embed element with the source being the filename. This works, but only when I directly type in " https://mmgamez.github.io/play.html?game.swf ". If I click on the link that I put on the "Games" page for a game, it won't pop up (at least for me on ChromeOS).

TLDR/Simplified version:

If I type in https://mmgamez.github.io/play.html?RiddleSchool1.swf in my URL bar the game Riddle School 1 will show up and be playable. If I go to the 'Games' tab on my website and click on the link "Riddle School 1" the game will not show up at all.

Heres how I embed my games using a "url variable":


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
var game = getUrlVars()["game"];

function embedFile(){
var el = document.getElementById("gamePlace");
var emb = document.createElement("embed");
    emb.setAttribute("width", "800");
    emb.setAttribute("height", "600");
    emb.setAttribute("allowfullscreen", "true");
    emb.setAttribute('src', "games/"+game.toString());
    emb.setAttribute('type', 'application/x-shockwave-flash')
    el.appendChild(emb);
}

Why in the world does this happen and what can I do to fix it?

It looks like your getUrlVars() function is working correctly. However, with query string values, you are looking for key/value pairs (eg key1=value1&key2=value2).

Looking at this line

var game = getUrlVars()["game"];

I think you just need to change your links to add "game" as the key:

<a href="https://mmgamez.github.io/play.html?game=RiddleSchool1.swf">RiddleSchool1</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM