Rectangle 27 1115

If you can have an actual URL that makes sense use that as the HREF. The onclick won't fire if someone middle-clicks on your link to open a new tab or if they have JavaScript disabled.

If that is not possible, then you should at least inject the anchor tag into the document with JavaScript and the appropriate click event handlers.

I realize this isn't always possible, but in my opinion it should be striven for in developing any public website.

Check out Unobtrusive JavaScript and Progressive enhancement (both Wikipedia).

Can you give an example of how one might "inject the anchor tag into the document with JavaScript and the appropriate click event handlers"?

Why inject the anchor tag at all if it is merely meant to be used to allow an element to be a) clicked b) display the "pointer" cursor. It seems irrelevant to even inject an anchor tag in this scenario.

+1 for neither, but there are over approaches. Fact remains use of either javascript: or # are both bad practices and not to be encouraged. Nice articles btw.

@crush: I wouldn't use a div or span either; frankly, this seems like the very thing <button> was made for. But agreed: it probably shouldn't be specifically an <a> if there's no suitable place for it to link to.

@cHao Button is perhaps the best choice. I suppose the main point is that the HTML element shouldn't be selected based on its default presentation appearance, but rather its significance to the structure of the document.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 1114

If you can have an actual URL that makes sense use that as the HREF. The onclick won't fire if someone middle-clicks on your link to open a new tab or if they have JavaScript disabled.

If that is not possible, then you should at least inject the anchor tag into the document with JavaScript and the appropriate click event handlers.

I realize this isn't always possible, but in my opinion it should be striven for in developing any public website.

Check out Unobtrusive JavaScript and Progressive enhancement (both Wikipedia).

Can you give an example of how one might "inject the anchor tag into the document with JavaScript and the appropriate click event handlers"?

Why inject the anchor tag at all if it is merely meant to be used to allow an element to be a) clicked b) display the "pointer" cursor. It seems irrelevant to even inject an anchor tag in this scenario.

+1 for neither, but there are over approaches. Fact remains use of either javascript: or # are both bad practices and not to be encouraged. Nice articles btw.

@crush: I wouldn't use a div or span either; frankly, this seems like the very thing <button> was made for. But agreed: it probably shouldn't be specifically an <a> if there's no suitable place for it to link to.

@cHao Button is perhaps the best choice. I suppose the main point is that the HTML element shouldn't be selected based on its default presentation appearance, but rather its significance to the structure of the document.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 1114

If you can have an actual URL that makes sense use that as the HREF. The onclick won't fire if someone middle-clicks on your link to open a new tab or if they have JavaScript disabled.

If that is not possible, then you should at least inject the anchor tag into the document with JavaScript and the appropriate click event handlers.

I realize this isn't always possible, but in my opinion it should be striven for in developing any public website.

Check out Unobtrusive JavaScript and Progressive enhancement (both Wikipedia).

Can you give an example of how one might "inject the anchor tag into the document with JavaScript and the appropriate click event handlers"?

Why inject the anchor tag at all if it is merely meant to be used to allow an element to be a) clicked b) display the "pointer" cursor. It seems irrelevant to even inject an anchor tag in this scenario.

+1 for neither, but there are over approaches. Fact remains use of either javascript: or # are both bad practices and not to be encouraged. Nice articles btw.

@crush: I wouldn't use a div or span either; frankly, this seems like the very thing <button> was made for. But agreed: it probably shouldn't be specifically an <a> if there's no suitable place for it to link to.

@cHao Button is perhaps the best choice. I suppose the main point is that the HTML element shouldn't be selected based on its default presentation appearance, but rather its significance to the structure of the document.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 1

Web context cannot open chrome-extension:// or chrome:// links due to security restrictions. The idea is that simply opening the page may perform some action with higher privileges than the webpage.

externally_connectable

(assuming you control both the extension and the webpage)

  • You declare in your extension's manifest that your domain, https://example.com/, can call the extension.
  • From your webpage code, you check that chrome.runtime.sendMessage is available - that means an extension that is ready to listen is installed.
  • You then call chrome.runtime.sendMessage with the extension ID and the request to do something (i.e. open an extension page).
  • In the extension's background page, receive the message with chrome.runtime.onMessageExternal and do something, e.g. open the page with chrome.tabs API.

If you control the extension, but not the website, and you're adding the button from a content script, you should assign a handler that calls chrome.runtime.sendMessage and, again, handle it from the background page (which can open an extension page, unlike the content script). This time though it's going to be chrome.runtime.onMessage event.

If you don't control the extension, there's nothing you can do for the security reason above.

Are you sure @Xan? Why I can open chrome-extension urls through a test html page?

I don't know. If I try it from the web context, it opens about:blank. Are you testing with a local file:/// URL? That may allow it. web_accessible_resources may also be involved.

yes a file:/// url and an unpacked extension path.

Try editing a link on a web origin page, and make sure that the page is not web-accessible.

about:blank

javascript - Open chrome extension in html - Stack Overflow

javascript html google-chrome-extension
Rectangle 27 51

sure about that?

I am also wishing I could programmatically open a select for keyboard users. In Firefox the change event doesn't fire until the select loses focus, and if the menu isn't actually open, it doesn't select anything when you tab off. LAME-O!

javascript - Is it possible to use JS to open an HTML select to show i...

javascript html-select
Rectangle 27 8

window.open('url to open','window name','toolbar=no');

Remeber that users can force their browser to always show toolbars and status bars.

javascript - Open html link in new window with no browser toolbars - S...

javascript html
Rectangle 27 43

We had the same issue too many complaints from our customers. We traced it to the Excel OFffice Security patch KB3115262 - https://support.microsoft.com/en-us/kb/3115262 Which came out July 12, 2016

To work around the issue, we had customers make a change in their Excel to allow files from the internet.

3) Go to Protected View. there are 3 options that show that were all clicked

We uncheck the first option that reads -- "Enable Protected View for files originating from the Internet"

That fixed the issue. Perhaps not the best solution. I'm not sure why this particular KB broke this but I think perhaps the fact the file format is not expected and this setting are conflicting with each other.

On a computer I have that doesn't have this KB installed, all those are checked and it still works fine (not blank but prompts file format is different) and I think shows in protected view.

What led us to fact its not the content of the file is we noticed if we resave the file in notepad or notepad++ without making any changes, the file behaves fine so excel must be reading some property of the file rather than the content to block it.

Thank you! This helped a lot. For german office users: Datei -> Optionen -> Trust Center -> Einstellungen fr das Trust Center -> Geschtzte Ansicht -> Haken entfernen bei "Geschtzte Ansicht bei Dateien aus dem Internet aktivieren"

Several customer emails and a couple hours of work in, and I find this. Thank you so much!

javascript - Export HTML Table to Excel- Doesn't Open in Office 2010 -...

javascript excel
Rectangle 27 23

dropDown = function (elementId) {
    var dropdown = document.getElementById(elementId);
    try {
        showDropdown(dropdown);
    } catch(e) {

    }
    return false;
};

showDropdown = function (element) {
    var event;
    event = document.createEvent('MouseEvents');
    event.initMouseEvent('mousedown', true, true, window);
    element.dispatchEvent(event);
};

FYI: It did work for me on Chrome 35.0.1916.153 Make sure you're passing the actual element and not a jQuery obj.

javascript - Is it possible to use JS to open an HTML select to show i...

javascript html-select
Rectangle 27 20

I use this... but it requires the user to click on the select box...

Here are the 2 javascript functions

function expand(obj)
{
    obj.size = 5;
}
function unexpand(obj)
{
    obj.size = 1;
}

then i create the select box

<select id="test" multiple="multiple" name="foo" onFocus="expand(this)" onBlur="unexpand(this)">
<option >option1</option>
<option >option2</option>
<option >option3</option>
<option >option4</option>
<option >option5</option>
</select>

I know this code is a little late, but i hope it helps someone who had the same problem as me.

ps/fyi i have not tested the code above (i create my select box dynamically), and the code i did write was only tested in FireFox.

+1, done similar things before, I believe I also set it's position to absolute when it was expanded so it didn't break document flow, and back to block when it was collapsed.

In my case it wouldn't solve the problem exactly, but it would be an option. +1

@DarrylHein: In this case, it's not just an option, it's 5 <option>s ...

very crafty, thanks.

javascript - Is it possible to use JS to open an HTML select to show i...

javascript html-select
Rectangle 27 4

The solution I present is safe, simple and compatible with Internet Explorer, FireFox and Chrome.

This approach is new and complete. I not found nothing equal to that solution on the internet. Is simple, cross-browser (Internet Explorer, Chrome and Firefox), preserves the layout, use the select itself and is easy to use.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CustonSelect</title>
<script type="text/javascript" src="./jquery-1.3.2.js"></script>
<script type="text/javascript" src="./CustomSelect.js"></script>
</head>
<div id="testDiv"></div>
<body>
    <table>
        <tr>
            <td>
                <select id="Select0" >
                    <option value="0000">0000</option>
                    <option value="0001">0001</option>
                    <option value="0002">0002</option>
                    <option value="0003">0003</option>
                    <option value="0004">0004</option>
                    <option value="0005">0005</option>
                    <option value="0006">0006</option>
                    <option value="0007">0007</option>
                    <option value="0008">0008</option>
                    <option value="0009">0009</option>
                    <option value="0010">0010</option>
                    <option value="0011">0011</option>
                    <option value="0012">0012</option>
                    <option value="0013">0013</option>
                    <option value="0014">0014</option>
                    <option value="0015">0015</option>
                    <option value="0016">0016</option>
                    <option value="0017">0017</option>
                    <option value="0018">0018</option>
                    <option value="0019">0019</option>
                    <option value="0020">0020</option>
                    <option value="0021">0021</option>
                    <option value="0022">0022</option>
                    <option value="0023">0023</option>
                    <option value="0024">0024</option>
                    <option value="0025">0025</option>
                    <option value="0026">0026</option>
                    <option value="0027">0027</option>
                    <option value="0028">0028</option>
                    <option value="0029">0029</option>
                    <option value="0030">0030</option>
                    <option value="0031">0031</option>
                    <option value="0032">0032</option>
                    <option value="0033">0033</option>
                    <option value="0034">0034</option>
                    <option value="0035">0035</option>
                    <option value="0036">0036</option>
                    <option value="0037">0037</option>
                    <option value="0038">0038</option>
                    <option value="0039">0039</option>
                    <option value="0040">0040</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <select id="Select1" >
                    <option value="0000">0000</option>
                    <option value="0001">0001</option>
                    <option value="0002">0002</option>
                    <option value="0003">0003</option>
                    <option value="0004">0004</option>
                    <option value="0005">0005</option>
                    <option value="0006">0006</option>
                    <option value="0007">0007</option>
                    <option value="0008">0008</option>
                    <option value="0009">0009</option>
                    <option value="0010">0010</option>
                    <option value="0011">0011</option>
                    <option value="0012">0012</option>
                    <option value="0013">0013</option>
                    <option value="0014">0014</option>
                    <option value="0015">0015</option>
                    <option value="0016">0016</option>
                    <option value="0017">0017</option>
                    <option value="0018">0018</option>
                    <option value="0019">0019</option>
                    <option value="0020">0020</option>
                    <option value="0021">0021</option>
                    <option value="0022">0022</option>
                    <option value="0023">0023</option>
                    <option value="0024">0024</option>
                    <option value="0025">0025</option>
                    <option value="0026">0026</option>
                    <option value="0027">0027</option>
                    <option value="0028">0028</option>
                    <option value="0029">0029</option>
                    <option value="0030">0030</option>
                    <option value="0031">0031</option>
                    <option value="0032">0032</option>
                    <option value="0033">0033</option>
                    <option value="0034">0034</option>
                    <option value="0035">0035</option>
                    <option value="0036">0036</option>
                    <option value="0037">0037</option>
                    <option value="0038">0038</option>
                    <option value="0039">0039</option>
                    <option value="0040">0040</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <select id="Select2" >
                    <option value="0000">0000</option>
                    <option value="0001">0001</option>
                    <option value="0002">0002</option>
                    <option value="0003">0003</option>
                    <option value="0004">0004</option>
                    <option value="0005">0005</option>
                    <option value="0006">0006</option>
                    <option value="0007">0007</option>
                    <option value="0008">0008</option>
                    <option value="0009">0009</option>
                    <option value="0010">0010</option>
                    <option value="0011">0011</option>
                    <option value="0012">0012</option>
                    <option value="0013">0013</option>
                    <option value="0014">0014</option>
                    <option value="0015">0015</option>
                    <option value="0016">0016</option>
                    <option value="0017">0017</option>
                    <option value="0018">0018</option>
                    <option value="0019">0019</option>
                    <option value="0020">0020</option>
                    <option value="0021">0021</option>
                    <option value="0022">0022</option>
                    <option value="0023">0023</option>
                    <option value="0024">0024</option>
                    <option value="0025">0025</option>
                    <option value="0026">0026</option>
                    <option value="0027">0027</option>
                    <option value="0028">0028</option>
                    <option value="0029">0029</option>
                    <option value="0030">0030</option>
                    <option value="0031">0031</option>
                    <option value="0032">0032</option>
                    <option value="0033">0033</option>
                    <option value="0034">0034</option>
                    <option value="0035">0035</option>
                    <option value="0036">0036</option>
                    <option value="0037">0037</option>
                    <option value="0038">0038</option>
                    <option value="0039">0039</option>
                    <option value="0040">0040</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <select id="Select3" >
                    <option value="0000">0000</option>
                    <option value="0001">0001</option>
                    <option value="0002">0002</option>
                    <option value="0003">0003</option>
                    <option value="0004">0004</option>
                    <option value="0005">0005</option>
                    <option value="0006">0006</option>
                    <option value="0007">0007</option>
                    <option value="0008">0008</option>
                    <option value="0009">0009</option>
                    <option value="0010">0010</option>
                    <option value="0011">0011</option>
                    <option value="0012">0012</option>
                    <option value="0013">0013</option>
                    <option value="0014">0014</option>
                    <option value="0015">0015</option>
                    <option value="0016">0016</option>
                    <option value="0017">0017</option>
                    <option value="0018">0018</option>
                    <option value="0019">0019</option>
                    <option value="0020">0020</option>
                    <option value="0021">0021</option>
                    <option value="0022">0022</option>
                    <option value="0023">0023</option>
                    <option value="0024">0024</option>
                    <option value="0025">0025</option>
                    <option value="0026">0026</option>
                    <option value="0027">0027</option>
                    <option value="0028">0028</option>
                    <option value="0029">0029</option>
                    <option value="0030">0030</option>
                    <option value="0031">0031</option>
                    <option value="0032">0032</option>
                    <option value="0033">0033</option>
                    <option value="0034">0034</option>
                    <option value="0035">0035</option>
                    <option value="0036">0036</option>
                    <option value="0037">0037</option>
                    <option value="0038">0038</option>
                    <option value="0039">0039</option>
                    <option value="0040">0040</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <select id="Select4" >
                    <option value="0000">0000</option>
                    <option value="0001">0001</option>
                    <option value="0002">0002</option>
                    <option value="0003">0003</option>
                    <option value="0004">0004</option>
                    <option value="0005">0005</option>
                    <option value="0006">0006</option>
                    <option value="0007">0007</option>
                    <option value="0008">0008</option>
                    <option value="0009">0009</option>
                    <option value="0010">0010</option>
                    <option value="0011">0011</option>
                    <option value="0012">0012</option>
                    <option value="0013">0013</option>
                    <option value="0014">0014</option>
                    <option value="0015">0015</option>
                    <option value="0016">0016</option>
                    <option value="0017">0017</option>
                    <option value="0018">0018</option>
                    <option value="0019">0019</option>
                    <option value="0020">0020</option>
                    <option value="0021">0021</option>
                    <option value="0022">0022</option>
                    <option value="0023">0023</option>
                    <option value="0024">0024</option>
                    <option value="0025">0025</option>
                    <option value="0026">0026</option>
                    <option value="0027">0027</option>
                    <option value="0028">0028</option>
                    <option value="0029">0029</option>
                    <option value="0030">0030</option>
                    <option value="0031">0031</option>
                    <option value="0032">0032</option>
                    <option value="0033">0033</option>
                    <option value="0034">0034</option>
                    <option value="0035">0035</option>
                    <option value="0036">0036</option>
                    <option value="0037">0037</option>
                    <option value="0038">0038</option>
                    <option value="0039">0039</option>
                    <option value="0040">0040</option>
                </select>
            </td>
        </tr>
    </table>
    <input type="button" id="Button0" value="MoveLayout!"/>
</body>
</html>
var customSelectFields = new Array();


// Note: The list of selects to be modified! By Questor
customSelectFields[0] = "Select0";
customSelectFields[1] = "Select1";
customSelectFields[2] = "Select2";
customSelectFields[3] = "Select3";
customSelectFields[4] = "Select4";

$(document).ready(function()
{


    //Note: To debug! By Questor
    $("#Button0").click(function(event){ AddTestDiv(); });

    StartUpCustomSelect(null);  

});


//Note: To test! By Questor
function AddTestDiv()
{
    $("#testDiv").append("<div style=\"width:100px;height:100px;\"></div>");
}


//Note: Startup selects customization scheme! By Questor
function StartUpCustomSelect(what)
{

    for (i = 0; i < customSelectFields.length; i++)
    {

        $("#" + customSelectFields[i] + "").click(function(event){ UpCustomSelect(this); });
        $("#" + customSelectFields[i] + "").wrap("<div id=\"selectDiv_" + customSelectFields[i] + "\" onmouseover=\"BlockCustomSelectAgain();\" status=\"CLOSED\"></div>").parent().after("<div id=\"coverSelectDiv_" + customSelectFields[i] + "\" onclick=\"UpOrDownCustomSelect(this);\" onmouseover=\"BlockCustomSelectAgain();\"></div>");


        //Note: Avoid breaking the layout when the CSS is modified from "position" to "absolute" on the select! By Questor
        $("#" + customSelectFields[i] + "").parent().css({'width': $("#" + customSelectFields[i] + "")[0].offsetWidth + 'px', 'height': $("#" + customSelectFields[i] + "")[0].offsetHeight + 'px'});

        BlockCustomSelect($("#" + customSelectFields[i] + ""));

    }
}


//Note: Repositions the div that covers the select using the "onmouseover" event so 
//Note: if element on the screen move the div always stand over it (recalculate! By Questor
function BlockCustomSelectAgain(what)
{
    for (i = 0; i < customSelectFields.length; i++)
    {
        if($("#" + customSelectFields[i] + "").parent().attr("status") == "CLOSED")
        {
            BlockCustomSelect($("#" + customSelectFields[i] + ""));
        }
    }
}


//Note: Does not allow the select to be clicked or clickable! By Questor
function BlockCustomSelect(what)
{

    var coverSelectDiv = $(what).parent().next();


    //Note: Ensures the integrity of the div style! By Questor
    $(coverSelectDiv).removeAttr('style');


    //Note: To resolve compatibility issues! By Questor
    var backgroundValue = "";
    var filerValue = "";
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        backgroundValue = 'url(fakeimage)';
        filerValue = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=\'scale\', src=\'fakeimage\' )';
    }


    //Note: To debug! By Questor
    //'border': '5px #000 solid',

    $(coverSelectDiv).css({
        'position': 'absolute', 
        'top': $(what).offset().top + 'px', 
        'left': $(what).offset().left + 'px', 
        'width': $(what)[0].offsetWidth + 'px', 
        'height': $(what)[0].offsetHeight + 'px', 
        'background': backgroundValue,
        '-moz-background-size':'cover',
        '-webkit-background-size':'cover',
        'background-size':'cover',
        'filer': filerValue
    });

}


//Note: Allow the select to be clicked or clickable! By Questor
function ReleaseCustomSelect(what)
{

    var coverSelectDiv = $(what).parent().next();

    $(coverSelectDiv).removeAttr('style');
    $(coverSelectDiv).css({'display': 'none'});

}


//Note: Open the select! By Questor
function DownCustomSelect(what)
{


    //Note: Avoid breaking the layout. Avoid that select events be overwritten by the others! By Questor
    $(what).css({
        'position': 'absolute', 
        'z-index': '100'
    });


    //Note: Open dropdown! By Questor
    $(what).attr("size","10");

    ReleaseCustomSelect(what);


    //Note: Avoids the side-effect of the select loses focus.! By Questor
    $(what).focus();


    //Note: Allows you to select elements using the enter key when the select is on focus! By Questor
    $(what).keyup(function(e){
        if(e.keyCode == 13)
        {
            UpCustomSelect(what);
        }
    });


    //Note: Closes the select when loses focus! By Questor
    $(what).blur(function(e){
        UpCustomSelect(what);
    });

    $(what).parent().attr("status", "OPENED");

}


//Note: Close the select! By Questor
function UpCustomSelect(what)
{

    $(what).css("position","static");


    //Note: Close dropdown! By Questor
    $(what).attr("size","1");

    BlockCustomSelect(what);

    $(what).parent().attr("status", "CLOSED");

}


//Note: Closes or opens the select depending on the current status! By Questor
function UpOrDownCustomSelect(what)
{

    var customizedSelect = $($(what).prev().children()[0]);

    if($(what).prev().attr("status") == "CLOSED")
    {
        DownCustomSelect(customizedSelect);
    }
    else if($(what).prev().attr("status") == "OPENED")
    {
        UpCustomSelect(customizedSelect);
    }

}

I created a jsfiddle with this, and it does not appear to be working... jsfiddle.net/rL53xj11

@JonCoombs Yes, it works. I have a client application where this solution is running perfectly. Note, however, that this code is already 2 years old. I recommend testing on an html file on your local machine yourself. Also note the version of your jquery.

javascript - Is it possible to use JS to open an HTML select to show i...

javascript html-select
Rectangle 27 18

I didn't want the select box to show until the user clicked on some plain HTML. So I overlayed the select element with opacity=.01. Upon clicking, I changed it back to opacity=100. This allowed me to hide the select, and when the user clicked the text the select appeared with the options showing.

Isn't that just hiding/showing the actual select field? The question is about showing/opening the options list.

My method does show show/open the options list along with the select box itself. The only way to open the options list is to have the select box clicked. Which I accomplished by invisibly overlaying the select on top of some target text.

This was the only solution capable of styling the select menu button in the buggy WebView of Android. Thank you!

Thanks, was looking for this for a couple of hours. This should be the accepted answer!

This doesn't answer the question, but it DOES present a workaround for the case where you want to style a <select> element while still providing the native control, which is a reason why you might want to open the options drop down using js. If you set the opacity of the <select> to 0, when the user clicks the invisible select, the drop down of options will appear as normal.

javascript - Is it possible to use JS to open an HTML select to show i...

javascript html-select
Rectangle 27 114

If you want to create and submit your form from Javascript as is in your question and you want to create popup window with custom features I propose this solution (I put comments above the lines i added):

var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "test.jsp");

// setting form target to a window named 'formresult'
form.setAttribute("target", "formresult");

var hiddenField = document.createElement("input");              
hiddenField.setAttribute("name", "id");
hiddenField.setAttribute("value", "bob");
form.appendChild(hiddenField);
document.body.appendChild(form);

// creating the 'formresult' window with custom features prior to submitting the form
window.open('test.html', 'formresult', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');

form.submit();

+1 Better than the accepted answer, since it actually puts the result in the popup with the options the OP wanted.

Answered exactly what was asked. Thanks!

@mjaggard: What did you add? It might be worth suggesting an edit to this answer.

@SaurabhNanda As far as I can tell, target attribute on form element is not deprecated in HTML 4.01 Transitional and apparently is to stay in HTML 5.

A warning: I had to attach my form to the document body before this would work (in FF 17). Creating a fragment and trying to sumbit it didn't work.

html - Javascript Post on Form Submit open a new window - Stack Overfl...

javascript html post
Rectangle 27 5

console.log(JSON.stringify({
  isAndroid: /Android/.test(navigator.userAgent),
  isCordova: !!window.cordova,
  isEdge: /Edge/.test(navigator.userAgent),
  isFirefox: /Firefox/.test(navigator.userAgent),
  isChrome: /Google Inc/.test(navigator.vendor),
  isChromeIOS: /CriOS/.test(navigator.userAgent),
  isChromiumBased: !!window.chrome && !/Edge/.test(navigator.userAgent),
  isIE: /Trident/.test(navigator.userAgent),
  isIOS: /(iPhone|iPad|iPod)/.test(navigator.platform),
  isOpera: /OPR/.test(navigator.userAgent),
  isSafari: /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent),
  isTouchScreen: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
  isWebComponentsSupported: 'registerElement' in document && 'import' in document.createElement('link') && 'content' in document.createElement('template')
}, null, '  '));

html - JavaScript: How to find out if the user browser is Chrome? - St...

javascript html google-chrome browser-detection
Rectangle 27 20

I choose use javascript:void(0), because use this could prevent right click to open the content menu.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 20

I choose use javascript:void(0), because use this could prevent right click to open the content menu.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 20

I choose use javascript:void(0), because use this could prevent right click to open the content menu.

html - Which "href" value should I use for JavaScript links, "#" or "j...

javascript html performance optimization href
Rectangle 27 29

Create these files in the extension folder:

You can see this in action by:

  • Creating a new extension folder.
  • Create the 5 files as specified below.
  • Load the unpacked extension (You can see similar steps in this answer.)
  • Reload this page in Chrome; you'll see the "Hello World" page, embedded at the top.
{
    "manifest_version":         2,
    "content_scripts":          [ {
        "js":       [ "iframeInjector.js" ],
        "matches":  [   "https://stackoverflow.com/questions/*"
        ]
    } ],
    "description":              "Inject a complete, premade web page",
    "name":                     "Inject whole web page",
    "version":                  "1",
    "web_accessible_resources": ["Embedded_Hello_world.htm"]
}
var iFrame  = document.createElement ("iframe");
iFrame.src  = chrome.extension.getURL ("Embedded_Hello_world.htm");

document.body.insertBefore (iFrame, document.body.firstChild);
<!DOCTYPE html>
<html><head>
    <title>Embedded Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <link href="HelloWorld.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="HelloWorld.js"></script>
</head><body>
<p>Hello World!</p>
</body></html>
body {
    color:              red;
    background-color:   lightgreen;
}
$(document).ready (jQueryMain);

function jQueryMain () {
    $("body").append ('<p>Added by jQuery</p>');
}

Thanks for this answer. But the problem is that I need a lot of communication between the page and my script. I think that messaging through an iFrame each UI event (I need to detect mouvemoves, keystrokes etc) is gonna be really complicated.

@Malharhak did you find a answer? how communication between the iframe and content script?

Sorry I really don't remember, this was a long time ago. I did find a way to make everything work though. I don't think I went with iFrames though, I was just injecting html in js.

Suppose if i need to query from background.js from this html. How do i do? I get chrome.tabs.query as undefined? Could you please help?

javascript - Adding complex HTML using a Chrome content script - Stack...

javascript html5 google-chrome google-chrome-extension content-script
Rectangle 27 28

Create these files in the extension folder:

You can see this in action by:

  • Creating a new extension folder.
  • Create the 5 files as specified below.
  • Load the unpacked extension (You can see similar steps in this answer.)
  • Reload this page in Chrome; you'll see the "Hello World" page, embedded at the top.
{
    "manifest_version":         2,
    "content_scripts":          [ {
        "js":       [ "iframeInjector.js" ],
        "matches":  [   "https://stackoverflow.com/questions/*"
        ]
    } ],
    "description":              "Inject a complete, premade web page",
    "name":                     "Inject whole web page",
    "version":                  "1",
    "web_accessible_resources": ["Embedded_Hello_world.htm"]
}
var iFrame  = document.createElement ("iframe");
iFrame.src  = chrome.extension.getURL ("Embedded_Hello_world.htm");

document.body.insertBefore (iFrame, document.body.firstChild);
<!DOCTYPE html>
<html><head>
    <title>Embedded Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <link href="HelloWorld.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="HelloWorld.js"></script>
</head><body>
<p>Hello World!</p>
</body></html>
body {
    color:              red;
    background-color:   lightgreen;
}
$(document).ready (jQueryMain);

function jQueryMain () {
    $("body").append ('<p>Added by jQuery</p>');
}

Thanks for this answer. But the problem is that I need a lot of communication between the page and my script. I think that messaging through an iFrame each UI event (I need to detect mouvemoves, keystrokes etc) is gonna be really complicated.

@Malharhak did you find a answer? how communication between the iframe and content script?

Sorry I really don't remember, this was a long time ago. I did find a way to make everything work though. I don't think I went with iFrames though, I was just injecting html in js.

Suppose if i need to query from background.js from this html. How do i do? I get chrome.tabs.query as undefined? Could you please help?

javascript - Adding complex HTML using a Chrome content script - Stack...

javascript html5 google-chrome google-chrome-extension content-script
Rectangle 27 28

Create these files in the extension folder:

You can see this in action by:

  • Creating a new extension folder.
  • Create the 5 files as specified below.
  • Load the unpacked extension (You can see similar steps in this answer.)
  • Reload this page in Chrome; you'll see the "Hello World" page, embedded at the top.
{
    "manifest_version":         2,
    "content_scripts":          [ {
        "js":       [ "iframeInjector.js" ],
        "matches":  [   "https://stackoverflow.com/questions/*"
        ]
    } ],
    "description":              "Inject a complete, premade web page",
    "name":                     "Inject whole web page",
    "version":                  "1",
    "web_accessible_resources": ["Embedded_Hello_world.htm"]
}
var iFrame  = document.createElement ("iframe");
iFrame.src  = chrome.extension.getURL ("Embedded_Hello_world.htm");

document.body.insertBefore (iFrame, document.body.firstChild);
<!DOCTYPE html>
<html><head>
    <title>Embedded Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <link href="HelloWorld.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="HelloWorld.js"></script>
</head><body>
<p>Hello World!</p>
</body></html>
body {
    color:              red;
    background-color:   lightgreen;
}
$(document).ready (jQueryMain);

function jQueryMain () {
    $("body").append ('<p>Added by jQuery</p>');
}

Thanks for this answer. But the problem is that I need a lot of communication between the page and my script. I think that messaging through an iFrame each UI event (I need to detect mouvemoves, keystrokes etc) is gonna be really complicated.

@Malharhak did you find a answer? how communication between the iframe and content script?

Sorry I really don't remember, this was a long time ago. I did find a way to make everything work though. I don't think I went with iFrames though, I was just injecting html in js.

Suppose if i need to query from background.js from this html. How do i do? I get chrome.tabs.query as undefined? Could you please help?

javascript - Adding complex HTML using a Chrome content script - Stack...

javascript html5 google-chrome google-chrome-extension content-script
Rectangle 27 35

The web browser automatically focuses on the new tab, but you can call the focus back:

function openWindow( url )
{
  window.open(url, '_blank');
  window.focus();
}

<a href="http://www.example.com/" onclick="javascript:openWindow(this.href);return false;">Click Me</a>

Re focus: No in my experience. Not with Chrome, anyway (jsbin.com/aboluk/2) -- but they're not much harm in trying.

@T.J.Crowder: That actually does work for me in Chrome 15.

@josh3736: That's interesting. I'm still on 14 (on Linux) and it doesn't. So basically: You can't rely on it. :)

@T.J.Crowder: Works on Chrome 14 Windows and Chrome 15 Ubuntu. (Perhaps it's something strange going on in your setup?) At any rate, it doesn't work on any version of Firefox or Opera.

@T.J. Crowder Works for me with Chrome 14 on Ubuntu.

html - How to open new tab in JavaScript without switching to the new ...

javascript html