简体   繁体   中英

Redirect URL and run script on that page

Let's say I have a text box and a submit button on my page. After filling the text box and submitting, is it possible that I open a URL on new tab and then with JS run such code on that external page:

document.getElementById('input').value=val; // val is the data from my page
document.forms[0].submit();

so that data is submitted and user can directly see the results on that page.

I am not sure about runnig scripts on other pages but I would be glad if anyone can guide me generally which ways should I look for

Thanks in advance

Update 21/07: I tried to change my method to send form to the page that I want to redirect. So I created a model that stores url and form for that spesific address, because it differs, and may not have a form even.

After the form on my site is submitted, I send the model. And I put a JS function to be calles onload of body. So if there is data sent it will display the page in the iframe with this form generated is POSTed.

function onPageLoad() {
    if ('@Model' != null) {
        var model = @Html.Raw(Json.Encode(Model)); 
        if(model.postForm != null){
            document.getElementById("form-tab").innerHTML += model.postForm;
        }
    }
}

And the html part:

<form class="navbar-form navbar-right" role="search">
            <div class="form-group">
                <input type="text" class="form-control" name="datano" id="number"required>                           
            </div>
            <div id="form-tab"></div>
            <div class="col-md-offset-5">                    
                <button id="btnSubmit" type="submit" class="btn btn-default">Track</button>
            </div>
        </form>

My solution: After a week, I worked it out, maybe not the way I wanted but it does exactly what I wanted. I created another View that is empty, but has a place to generate HTML form here. So I passed as model the form here and just inserted as raw HTML into this HTML, and on load i called submit form function.

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