0

I am creating a simple 'in-browser' python IDE (here: coder.computerscienceuk.com/coder) and have a save button which saves the editor code to a new URL.

How can I program the save button so that when visiting the main website it opens the new URL on the same page (as it currently does) but if the button is clicked from within an iFrame (embedded on another site) it forces the new URL to open in a new window/tab?

Here is my button code:

function remix(private) {
    var private = private;
    var codedata = myCodeMirror.getValue();
    var uid = $js_userID;
    var ide = $js_ide;
    var response;
    
    if (private == 1 && uid == 0) {
        alert("You need to be logged in to save to a private URL");
    } else {
        $.ajax({
    type: "POST",
    url: {$js_url} + '/wp-content/plugins/CSUKCODER/remix.php',
        data: {"codedata" : codedata, "uid" : uid, "ide" : ide, "private" : private},
        success: function(data) {
            response = data;
            response = response.replace('"','');
            response = parseInt(response);
            hashids = new Hashids();
            id = hashids.encode(response);
            window.open($js_urlpage + String(id), '_self')
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
    };
}

<img src="$pluginRootPath/img/diskette.png" style="height:45px; padding-left:10px; cursor:pointer;" onclick="remix(0)" title="Save to URL">
sw123456
  • 3,339
  • 1
  • 24
  • 42
  • `var private = private;` doesn’t do anything and can be removed. `{$js_url} + '/wp-content/plugins/CSUKCODER/remix.php'` is `"[object Object]/wp-content/plugins/CSUKCODER/remix.php"`. Did you mean `\`${$js_url}/wp-content/plugins/CSUKCODER/remix.php\``? Please use `parseInt` [_with_ the second parameter, `10`](/q/16880327/4642212). Consider using [`Number`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number#Function_syntax) or [`parseFloat`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) instead. – Sebastian Simon Feb 02 '22 at 09:25
  • Thanks for your advice. I will look to tidy the code. But the code works fine as far as saving the code to the dB and passing back the ID of the last entry, in order to open the new URL for the saved code. My question relates to how I can get the button to open to a new page/window if it is clicked from an Iframe (from an external site). Regarding {$js_url} this is working - its has been encoded in JSON. – sw123456 Feb 02 '22 at 09:38
  • 1
    I'm not sure how JS objects are mapped into Python-in-browser, but see https://stackoverflow.com/questions/925039/detect-iframe-embedding-in-javascript – Noam Feb 02 '22 at 10:16
  • Does this answer your question? [Detect iFrame embedding in Javascript](https://stackoverflow.com/questions/925039/detect-iframe-embedding-in-javascript) – Noam Feb 02 '22 at 13:12

0 Answers0