admin管理员组文章数量:1334295
I have a textarea with a default value of http://
. Now when an user pastes in an url (if they don't know what they are doing, like most people) it es out like this http://
. I've seen a site that as soon as you have http://http://
it removes one via JavaScript.
I am not familiar with JavaScript, so can anyone help me?
I don't want to clear the field on focus only.
I have a textarea with a default value of http://
. Now when an user pastes in an url (if they don't know what they are doing, like most people) it es out like this http://http://www.google.
. I've seen a site that as soon as you have http://http://
it removes one via JavaScript.
I am not familiar with JavaScript, so can anyone help me?
I don't want to clear the field on focus only.
Share Improve this question edited Jul 16, 2010 at 7:54 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Jun 14, 2010 at 15:09 looloo 70710 silver badges22 bronze badges 4- Ajax is an entirely different story. It's "just" JavaScript. I've rephrased your question accordingly. – BalusC Commented Jun 14, 2010 at 15:13
- I edited my answer to include more info about when to call the function – marcgg Commented Jun 14, 2010 at 15:22
- I added an answer that catches ctrl+v pastes – jAndy Commented Jun 14, 2010 at 15:24
- Just want to add my appreciation that you are actually doing this at all... a lot of sites don't care about inconveniencing the user with issues like this – JoelFan Commented Jun 14, 2010 at 15:52
4 Answers
Reset to default 3Keeping it simple and using the replace function:
var url = "http://http://google.";
url = url.replace("http://http://","http://");
... this will basically replace the first string "http://http://"
by the second, "http://"
.
You'll need to call this when the content of the field change. For instance using jQuery:
$("#myfield").change(function(e){
$(this).val($(this).val().replace("http://http://","http://"));
});
without jQuery (not 100% sure about this):
document.getElementById("myfield").onChange = function(){
var val=document.getElementById("myfield").value;
document.getElementById("myfield").value = value.replace("http://http://","http://");
}
Unrelated but worth mentioning: This is not AJAX, it is simple javascript. Ajax is the term used when you try to have asynchronous munication with a server using the XMLHTTP object
Ajax (shorthand for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create interactive web applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page.
(via)
No Ajax at all to do that kind of magic.
This will do it:
$(function(){
$('textarea').bind('keydown', function(e){
var $this = $(this);
if(e.which === 86 && e.ctrlKey){
setTimeout(function(){
$this.val($this.val().replace(/http:\/\/http:\/\//,"http://"));
}, 1);
}
});
});
This will replace http://
on ctrl+v
if already exists.
You might also want to call the same routine on a change
event if an user uses
a contextmenu to paste.
You do not need ajax to do that just simple javascript can do the trick.
jQuery(document).ready(function(){
jQuery('#idofurtextfield').blur(function(){
jQuery(this).val(jQuery(this).val().replace(/(http:\/\/)\1/, '$1'));
});
});
Always nice to have a none regex option (saving those precious micro-seconds!):
var url = "http://http://google.";
url = url.substring(url.lastIndexOf("http://"));
// -> "http://google."
本文标签: javascriptRemove double http from input using jQueryStack Overflow
版权声明:本文标题:javascript - Remove double http: from input using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742356871a2459589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论