Skip to content Skip to sidebar Skip to footer

Why Cant I Transfer Data To Another Html Page

i am fully aware about the fact that there are dozens of questions that go along this but i felt that my scenario was a little different(Upto you to decide), Im trying to transfer

Solution 1:

There should be an error output from your code, but you somehow don't use debugging tool. I think this is good opportunity for you...

First, please change 'a tags href', like '#tut1.html', then click it, I think you don't transition to tut1.html. Also, when you transition to 'tut1.html', the url is the same what you want?

Let's try this.

'<a href="tut1.html"~''<a href="#tut1.html~'
 (Off course, e.preventDefault is also fine.)

and update the testJS function.

functiontestJS() {
    // There was syntax error. and the value format is "?name=value&name=value" usually.var b = document.getElementById('Name').value,
        c = document.getElementById('comment').value,
        url = 'http://E:\IIT\Homework\web1t/tut1.html?Name='+ encodeURIComponent(b) + '&Comment=' + encodeURIComponent(c);

    // if you don't use debugging tool, at least put an alert.alert(url);
    document.location.href = url;
}

tut1.html also need to update.

window.onload = function () {
    var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
        tmp = params[i].split('=');
        data[tmp[0]] = tmp[1];
    }
    // the name is wrong; you set name Name and Comment, when you create the url.document.getElementById('username').innerHTML = data.Name;
    document.getElementById('usercomment').innerHTML = data.Comment;
}

For your information. The first page's action is the same what you use simply

<form action='tut1.html' method='get'>
+
<inputtype="submit" value="submit">

In that case, the parameters send using its name and value.(?N=xxxx&C=yyyy) You don't need to create url including parameters yourself, unless there's a special reason.

Solution 2:

Just add a preventDefault in your testJS function like this

functiontestJS(e) {
    e.preventDefault();
    var b = document.getElementById('Name').value,
        document.getElementById('comment').value,
    url = 'http://E:\IIT\Homework\web1t/tut1.html?Name=' + encodeURIComponent(b);

    document.location.href = url;
}

As you are manually setting the url so you have to prevent the default a tag behaviour.

Post a Comment for "Why Cant I Transfer Data To Another Html Page"