...

View Full Version : Combine User input and URL to load page



abn3566
07-06-2011, 02:02 AM
I'm working in iWeb (I had to say that first). I need to create (using JAVASCRIPT) a text box with a button. When the button is clicked, the contents of the box are added to the URL (the url is in the code, probably a variable, for this example it is mydomain.wordpress.com/) and the url produced loads. For example, if the text "Hello" was printed in the text box once the button is clicked the page mydomain.wordpress.com/Hello is loaded.

DanInMa
07-06-2011, 02:12 AM
do you need the script to also create the button or does the button exist already for your example?

abn3566
07-06-2011, 02:13 AM
do you need the script to also create the button or does the button exist already for your example?

I need the code to create the button.

DanInMa
07-06-2011, 02:57 AM
function to create element, change the green text to reflect the base url of the site.

<script>

function add_my_button(id) {
input = document.createElement("input");
input.setAttribute("type", "button");
input.setAttribute("name", 'buttName_' + id);
input.setAttribute("id", 'buttID_' + id);
input.setAttribute("value", 'go here');
document.body.appendChild(input);
input2 = document.createElement("input");
input2.setAttribute("type", "text");
input2.setAttribute("name", 'textName_' + id);
input2.setAttribute("id", 'textID_' + id);
document.body.appendChild(input2);
input.onclick = function() {
var base_href= "http://127.0.0.1/"
var user_val = document.getElementById('textID_' + id).value;
window.location=base_href+user_val
}

}
</script>
this is how you include it.


<script>add_my_button("1");</script>

abn3566
07-06-2011, 03:14 AM
Thanks!

I tried it using the code below (I just put the button after the text box and changed its value to "Log In") and it is not working.



<script>

function add_my_button(id) {
input2 = document.createElement("input");
input2.setAttribute("type", "text");
input2.setAttribute("name", 'textName_' + id);
input2.setAttribute("id", 'textID_' + id);
document.body.appendChild(input2);
input = document.createElement("input");
input.setAttribute("type", "button");
input.setAttribute("name", 'buttName_' + id);
input.setAttribute("id", 'buttID_' + id);
input.setAttribute("value", 'Log In');
document.body.appendChild(input);
input.onclick = function() {
var base_href= "my.curtaingalleries.com/"
var user_val = document.getElementById('textID_' + id).value;
window.location=base_href+user_val
}

}
</script>

<script>add_my_button("1");</script>


my.curtaingalleries.com redirects to curtaingalleries.wordpress.com


It is currently at login.curtaingalleries.com and the test username is "admintesting"

low tech
07-06-2011, 03:32 AM
Hi

Look at your base href. Wrong: var base_href= "curtaingalleries.wordpress.com/"

example:


var base_href= "http://translate.google.com/"


LT

DanInMa
07-06-2011, 03:41 AM
then you should be using:


var base_href= "http://my.curtaingalleries.com/"

im still trying to figure out why the script has to create the button, seeing as how you have access to the html, but iot was fun to figure out.

DanInMa
07-06-2011, 03:52 AM
awesome now hes adding stuff that doesnt work directly to the html. I lvoe it when you spend your time on something and people dont even pay attention to the instructions.

abn3566
07-06-2011, 03:55 AM
Thanks for all of your help DanInMa and low tech. I am very close to having this entire problem solved. The button is now working except that it is opening the page in an iframe. How do I make the button open the new page in a new window and (obviously I'll just choose one method) in the same window?

DanInMa
07-06-2011, 03:59 AM
1. it didnt work the first time becuase you didnt put in the website address correctly.

2. the current code you have on the login page does absolutely nothing so I have no idea how you are getting a frame of any kind.

DanInMa
07-06-2011, 04:10 AM
ok your adding the code in some kind of widget iframe, I wont ask why.

correction this is it. have to do it his way since your adding the code to an iframe source file.



<script>

function add_my_button(id) {
input2 = document.createElement("input");
input2.setAttribute("type", "text");
input2.setAttribute("name", 'textName_' + id);
input2.setAttribute("id", 'textID_' + id);
document.body.appendChild(input2);
input = document.createElement("input");
input.setAttribute("type", "button");
input.setAttribute("name", 'buttName_' + id);
input.setAttribute("id", 'buttID_' + id);
input.setAttribute("value", 'Log In');
document.body.appendChild(input);
input.onclick = function() {
var base_href= "http://my.curtaingalleries.com/"
var user_val = document.getElementById('textID_' + id).value;
parent.document.location=base_href+user_val
}

}
</script>

DanInMa
07-06-2011, 04:23 AM
or
parent.document.location=base_href+user_val

abn3566
07-06-2011, 04:42 AM
or
parent.document.location=base_href+user_val

That did the trick! Thanks again!:)

I do have one more question though...how do I make the code work so that when I hit return, the button "is pressed"?

DanInMa
07-06-2011, 05:10 AM
That did the trick! Thanks again!:)

I do have one more question though...how do I make the code work so that when I hit return, the button "is pressed"?

you should have asked that in the beginning and mentioning your using the code in a frame would have helped immensely.

here use this.


<script>
function redirect(dataform) {

var base_href= "http://my.curtaingalleries.com/"
var user_val = dataform.txt1.value;
parent.document.location=base_href+user_val
return false
}
</script>
<form action="" name="myForm" id="myForm" onsubmit="return redirect(this)">
<input name="txt1" id="txt1" type="text" />
<input name="" type="submit" value="Login" /></form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum