Search code examples
javascriptgoogle-chromewindows-10microsoft-edge

Windows 10/11 - Command line to launch browser (Google Chrome or MS Edge) with Index page to open + a string to be passed to the JavaScript


Is there any way to launch either Google Chrone or MS Edge from command line with not only the page to be open (i.e. index.html) but also with a string to be passed to the JavaScript?

I found that this was doable in Internet Explorer using HTA but it appears such mechanism is not available in MS-Edge and (obviously) in Chrome.


Solution

  • Well... After some search and assistance from this site (see RVRX's answer here), I found the way to do what I needed. The command line to be included in a small bat script with only two lines:

    echo off
    start chrome file://d:/MyDir/index.html?rt=%cd% --disable-web-security --user-data-dir=%cd%
    

    being %cd% the folder in which the script sits.

    Within the Javascript, the value of rt is retrieved using the following code:

    var G_Project_Root ;
    var l_Passed_Params_String = window.location.search;
    var l_Parsed_Params = new URLSearchParams(l_Passed_Params_String);
    G_Project_Root = l_Parsed_Params.get('rt')