I need to check if a referrer has word "profile" i need to put profile/(.*?) in a var. How can I do it in js?
<script type="text/javascript">
var ref = document.referrer;
if( ~ref.indexOf("profile") ) {
alert('coincidence found!');
}
</script>
<script>
var str="Is this all there is?";
var patt1=/[^a-h]/g;
document.write(str.match(patt1));
</script>
Result :I,s, ,t,i,s, ,l,l, ,t,r, ,i,s,?
check link The [^abc] expression is used to find any character not between the brackets.
and this tooo link
var ref = document.referrer;
ref.match(/(?:profile).+/,(match)=> {
console.log(match)
})
Related
I need to restrict the URL postings in the textarea.
For this I used the code:
var url_act = jQuery("#area").val();
var matches = url_act.match(/http:/);
if (matches)
{
alert('You didn\'t have permission to post any url');
return false;
}
But if the content has any https: or url starts with www. is not restricted.
How to restrict if the content has any URL formats or not? If the URL is capital letters is not working.
Is there any way to do this?
Change your regex to,
var matches = url_act.match(/https?:|\bwww\./i);
i modifier helps to do a case-insensitive match.
try the following regex. It may help you.
http://www.regextester.com/20
I did not understand your question completely but this should help
Link to fiddle
$(document).ready(function() {
$('#check').click(function() {
var content = $('#myText').val();
var pattern = new RegExp("http:");
var result = '';
result = pattern.test(content) ? 'Invalid' : 'valid';
alert(result);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea id="myText" rows="10" cols="50"></textarea>
<br>
<button id="check">
Check
</button>
I use:
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var field1 = document.getElementById("field_wy4dm0");
field1.addEventListener("change", combineFields);
function combineFields() {
var val1 = field1.value;
var val1re = val1.match("/(.*)?")[1];
var str = document.getElementById("changeThisMovie").innerHTML;
var res = str.replace("yeC3AisTs2Y", val1re);
document.getElementById("changeThisMovie").innerHTML = res;
}
});
</script>
It works great to get a youtube video id which is located between "/" and "?" in the "val1" var and replace the old video id "yeC3AisTs2Y" that is located inside a div with the id="changeThisMovie" with the new one ("val1re").
The problem is it adds "?" in the end of the new video id so instead of getting:
/videoid?feature=embed...
I get:
/videoid??feature=embed...
How do i fix this?
Thanks!!!
I think you want to escape the ? In your regex to mean the literal ? symbol - regex will interpret as ignore previous block
So you could try as follows:
var val1re = val1.match("/(.*)\?")[1];
I have a small javascript issue; I want to reload page with a selected language option value as a get variable.
if I select EN language, the page reload with &lang=EN,
My problem is that I use concat so I get my_url&lang=EN&lang=FR&lang=SP ...
so when I select first EN then FR I want to get my_url&lang=FR not my_url&lang=EN&lang=FR
I want to replace the lang variable not only to add:
<select onchange="javascript:handleSelect(this)">
<option>DE</option>
<option>EN</option>
<option>FR</option>
<option>SP</option>
<option>NL</option>
<option>HR</option>
<option>PL</option>
<option>CZ</option>
</select>
<script type="text/javascript">
function handleSelect(elm)
{
window.location = window.location.href +"?lang="+elm.value;
}
</script>
Try this:
function handleSelect(elm)
{
var href = window.location.href;
if (href.indexOf("lang") > -1)
{
href = href.replace(/(lang)=\w+((?=[&])|)/, "lang="+elm.value);
}
else
{
var char = (href.indexOf("?") == -1 ? "?" : "&");
href+= char + "lang=" + elm.value;
}
window.location.href = href;
}
It should work with any kind of url keeping the params.
Fiddle. In the fiddle I'm using a div instead of the window.location.
try
window.location = window.location.pathname +"?lang="+elm.value;
You could use the replace function:
window.location = window.location.href.match(/lang=/) ? window.location.replace( /lang=(.*){2}/, 'lang=' + elm.value ) : window.location.href + '?lang=' + elm.value;
Reference: http://www.w3schools.com/jsref/jsref_replace.asp
If ?lang= exists, replace it with the new one.
If not, just add the lang parameter.
edit
I like the window.location.pathname solution from Dave Pile, this should be better than checking and replacing something.
edit2
var loc = 'http://test.de/?foo=bar'; // window.location.href;
var seperator = loc.match(/\?/) ? '&' : '?';
var elm = 'DE';
var url = loc.match(/lang/) ? loc.replace(/lang=(.*){2}/, 'lang' + elm ) : loc + seperator + 'lang=' + elm;
document.getElementById('result').innerHTML = url;
<div id="result"></div>
Look at this snippet, you have to change the loc so it should work, also change var url to window.location and elm to your language element.
It checks if parameters exists and change the seperator from ? to &, than if no lang is set, it will set it or if a lang is set, it will replace it.
function handleSelect(elm)
{
var href = window.location.href;
if (href.indexOf("lang") > -1)
window.location.href = href.replace(/(lang)=\w+((?=[&])|)/, "lang="+elm.value);
else
window.location = window.location.href +"&lang="+elm.value;
}
You could use
var currAddress = window.location.href;
var indexOfLang = currAddress.indexOf('lang=');
var tempAddress = currAddress.substring(indexOfLang, indexOfLang+7);
currAddress = currAddress.replace(tempAddress,'lang='+elm.value);
window.location = currAddress;
The number 7 is the length of substring - lang=EN.
Part of my code:
<p id="demo">{$value.file_name}</p>
<script type="text/javascript">
var str = document.getElementById("demo").innerHTML;
var res = str.replace("/var/www/html/biology/demo", "");
document.getElementById('para').innerHTML = res;
</script>
<a href="#para" id='para'>Download</a>
This part of the url will already be present: "a.b.c.d.edu/bio/cluster/"
$value.file_name contains "/var/www/html/biology/demo/files/mpijobs/107/mothership/data/job107_0_0_output.tif"
After the script, "para" contains the edited path which is "/files/mpijobs/107/mothership/data/job107_0_0_output.tif" (the removal of "/var/www/html/biology/demo")
The code:
Download
provides a clickable link to "a.b.c.d.edu/bio/cluster//var/www/html/biology/demo/files/mpijobs/107/mothership/data/job107_0_0_output.tif"
and what I want to do is replace "{$value.file_name}" inside the brackets with "para" (and what it represents) so that the download link is linked to
"a.b.c.d.edu/bio/cluster//files/mpijobs/107/mothership/data/job107_0_0_output.tif"
Sorry, I misunderstood.
If the a href attribute is set like so:
Download
You can use in the javascript:
str.setAttribute("href", res);
EDIT:
Ok I got it. Sorry about this strenuous exercise. Here's what you should write:
<p id="demo">{$value.file_name}</p>
<a href="#para" id='para'>Download</a>
<script type="text/javascript">
var str = document.getElementById("demo").innerHTML;
var res = str.replace("/var/www/html/biology/demo", "");
para = document.getElementById('para');
para.href = res;
</script>
In my jsp I use <%String base = (String)application.getAttribute("base");%>
I tried to use 'base' in javascript but not work. Below is my javascript:
<script>
var newBase = <%=base%>;
</script>
Can anyone help me to solve this?Thanks
This is the eplanation www.w3schools.com give for location object property pathname:
pathname: Sets or returns the path name of a URL
In our case the javascript file wich is in your context.
The first element is that pathname is the context
So you split the attribute (see the split method in javascript String) and return it.
This should do.
<script language='javascript'>
function servletContext() {
var sc = window.location.pathname.split( '/' );
return "/"+sc[1];
}
</script>
You can rather try it out like this ,
set the value to the hidden field ,
input type="hidden" id="hidVal" name="txt2" value="${base}"/>
And in your java script ,
<script>
var x = document.getElementById('hidVal').value;
alert(x);
</script>
Update :
var newBase = '<%=base%>';
You are missing the quotes to treat the value as string .
Hope this helps !!