I have these links :
Block 1 :
Adapters
Battery
After click on the link in Block1 , then I start to click on Block2:
Block 2 :
<img src ="/Content/Images/Top/searchbutton.png"/>
I can get the parameter value such as dep=56,cat=654 by using these jquery.
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null){
return "";
}else{
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function SearchClick() {
var cur_url = window.location.href;
var depId = getParameterByName("dep");
var catId = getParameterByName("cat");
var searchStr = getParameterByName("search");
var url_add = "";
if (depId != "") {
url_add += "&dep=" + depId;
}
window.location = "/Products?tab=2"+ url_add;
}
But now I exchanged the link in Block 1 with
<a href='javascript:void(0);' dep='" + work.ID + "'>" + work.ProName + "</a>
//it works well
So how can I get the value of dep in Block 1 by using javascript or jquery when I click on the the Block 2 link?
Thanks so much for all your answers.
It would be best if you put an ID on the A tag, like so:
<a id="myLink" href='javascript:void(0);' dep='" + work.ID + "'>" + work.ProName + "</a>
then you could use the following JQuery to get the value of work ID:
$("#myLink").attr("dep")
or plain old Javascript like so (assuming you've still got that ID on the A tag):
document.getElementById("myLink").getAttribute("dep")
EDIT: I've put it in the click for you, here is all my code:
<a id="block1Link" href='javascript:void(0);' dep="56">Adapters</a>
<br />
<br />
Click
<script type="text/javascript">
function SearchClick() {
var department = $("#block1Link").attr("dep");
alert(department);
}
</script>
Or if you want to use Javascript swap the var department line with this line:
var department = document.getElementById("block1Link").getAttribute("dep");
Related
I want to display first 10 characters afterword ... till .pdf or xlx or .docx match. Each result has link . when user click on perticular link it will redirect to matching file and file will be download. could anyone help me.
function createRowMultiresult(jobjects) {
var $div = $('<div class="chat Bot"></div>');
var $div2 = $('<div class="user-photo"><img src="{% static "Robot.jpg" %}" /></div>');
$div.append($div2);
var $tbl = $('<table style="width:100%;"></table>');
if (jobjects.length>1){
var $tr = $('<tr><td style="padding:5px;">Multiple results Found for your query. Please search with specific keyword</td></tr>');
$tbl.append($tr);
}
for(var x=0; x<jobjects.length;x++){
var currentobj = jobjects[x];
if (currentobj.ans != null){
if (currentobj.ans.indexOf("/AA") != -1){
var $tr = $('<tr><td style="padding:5px;"><a href="https://' + currentobj.ans +'" target="_blank" >Click Here for User Guide</a></td></tr>');
$tbl.append($tr);
}
else{
var $tr = $('<tr><td style="padding:5px;">' +(x+1)+'.'+ currentobj.ans.replace(/[^\w\s]/gi, "<br/>") +'</td> </tr>');
$tbl.append($tr);
if (currentobj.Pic.length>7){
var $tr = $("<tr><td style='text-align:center;'><img class='productpic' src='{% static '/Pictures/' %}" + currentobj.Pic +"' /></td></tr>");
$tbl.append($tr);
}
}
}
else if(currentobj.filename != null){
alert("Got there");
var $tr = $('<tr><td style="padding:5px;"><a href="https://' + currentobj.ans + '" target="_blank" >' + currentobj.filename.slice(0,10)+"...."+ +'</a></td> </tr>');
$tbl.append($tr);
}
}
var $par = $('<p class="chat-message"></p>');
$par.append($tbl);
$div.append($par);
$chatlog.append($div);
}
Each result has link.
Sample actual result:
insert_data_data_data.pdf
BE_guage_data_data.xlsx
BE_guage_data_data.docx
Expected output and when user click it will download :
insert_data....pdf
BE_guage.....xlsx
BE_guage.....docx
You can simply count character length and replace '...' after some point.
var displayName = ( currentobj.ans.length > 10 ) > ( currentobj.ans.substring( 0, 10 ) + '...' ) : currentobj.ans;
Now you can use this displayName variable for display.
in .substring() function first parameter is the starting point, and the second one is the endpoint.
According to the samples and the expected outputs, the simplest way is by using the replace function i.e.
result_str.replace(/_data_data\./, '...')
You can use a regex with capture groups. $n is the nth group.
var regex = /(\w{10})\w+(\.\w+)/;
var str = "insert_data_data_data.pdf";
console.log(str.replace(regex, "$1...$2"));
I want to remove the "http" if it is put in the url part of the input link, before the data is sent.
this my input code look onclick=
<input style=" outline: none;" type="button" onclick="formatText ('link:url');" class="btn btn-yeni" value="link"/>
This my javascript code (the received data is sent to another file and replaced.)
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value += '(' + tag + ')';
}
</script>
what i want to do If the input value is "link: http: //example.com" I would like to change it and post it as "link: example.com".
Can you try in your url string :
var result = url.replace(/(\w+:|^)\/\//, '');
result variable will hold "link : example.com" in place of "link : http://example.com"
Use the replace() function to replace part of a string.
function formatText(tag) {
var Field = document.getElementById('entry_girdi');
Field.value = Field.value.replace("http://", "");
Field.value += '(' + tag + ')';
}
I am having trouble in solving my previous post. I only got a solution which requires jQuery, but I want to use JavaScript instead.
Really need help on this.
Previous Post (For Reference) : HTML - How To Display & Direct Selected Values From Drop Down Menu To A New HTML Page
This is the first jquery codes.
<input type="submit" id="btngo" value="Go" />
<script type="text/javascript">
$(function() {
$("#btngo").bind("click", function() {
var url = "Page2.htm?foodbeverage=" + encodeURIComponent($("#foodbeverage").val()) + "&status=" + encodeURIComponent($("#status").val());
window.location.href = url;
});
});
</script>
This is the second jquery codes.
< script type = "text/javascript" >
var queryString = new Array();
$(function() {
if (queryString["foodbeverage"] != null && queryString["status"] != null) {
var data = "<b>Food Beverages:</b> " + queryString["foodbeverage"] + " <b>Dine Takeaway:</b> " + queryString["status"];
$("#showdata").html(data);
}
}); <
/script>
I've converted the first snippet to a native JS equivalent. I didn't add the status to the URL, it is just more of the same as getting the food beverage.
(function() {
/**
* Handles the click of the submit button.
*/
function onSubmitClicked(event) {
// Get the input element from the DOM.
var beverage = document.getElementById('foodbeverage'),
// Get the value from the element.
beverageValue = beverage.value,
// Construct the URL.
url = 'Page2.html?foodbeverage=' + encodeURIComponent(beverageValue) + '&status=test';
// Instead of going to the URL, log it to the console.
console.log('goto to URL: ', url);
}
// Get the button from the DOM.
var submitButton = document.getElementById('btngo');
// Add an event listener for the click event.
submitButton.addEventListener('click', onSubmitClicked);
})();
<label>
Beverage:
<input type="text" id="foodbeverage"/>
</label>
<input type="submit" id ="btngo" value="Go" />
For the second code snippet, add a div to the form/page. The JS function below will update the div value on window onload with parameters passed. Note that div is only updated if both the querystring paramaters are present.
<div id="showdata"></div>
<script type="text/javascript">
window.onload = passParameters;
//Function to update "showdata" div with URL Querystring parameter values
function passParameters() {
var foodbeverage = getParameterByName("foodbeverage");
var status = getParameterByName("status");
if (foodbeverage != null && status != null) {
var data = "<b>Food Beverages:</b> " + foodbeverage + " <b>Dine Takeaway:</b> " + status;
document.getElementById("showdata").innerHTML = data;
}
}
//Get URL parameter value
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>
fI'm developing a simple and small search in a Wordpress page using a $_GET variable in the url passed by javascript:
<script>
function pesquisar()
{
var pesquisar = document.getElementById('termo').value;
var caminho = document.URL+'&pesquisa='+pesquisar;
window.location = caminho;
}
</script>
<input type='text' id='termo'>
<input type='button' value='pesquisar' onclick='pesquisar()'>
So, the url to search is: MYURL/?page_id=51&pesquisa=test
Of course, the page_id is variable. If I search again, the URL is going to be: MYURL/?page_id=51&pesquisa=test&pesquisa=new, what is wrong.
How could I get just the MYURL/?page_id=51 using javascript? The window.location.pathname is not what I need.
Thanks.
Anything that searches naively will be vulnerable to problems like this: What if the URL is:
http://example.com/?pesquisa=test&page_id=51
You need to search for and remove the relevant query parameter:
var caminho = document.URL.replace(/([?&])pesquisa=[^&]+&?/,"$1")+"&pesquisa="+pesquisar;
Try this hopefully it should work
<script>
function pesquisar()
{
var pesquisar = document.getElementById('termo').value;
var caminho = location.protocol + '//' + location.host + location.pathname+'&pesquisa='+pesquisar;
window.location = caminho;
}
</script>
<input type='text' id='termo'>
<input type='button' value='pesquisar' onclick='pesquisar()'>
try this.
var a = document.URL;
var result = a.substr(0, a.indexOf('&'));
Resources:
Get current URL in web browser
how to grab substring before a specified character jquery or javascript
javascript:
<script type="text/javascript">
function get_query_param(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
window.onload = function() {
if (get_query_param("page_id") != null) {
alert(window.location.pathname + "?page_id=" + get_query_param('page_id'));
}
}
</script>
hope that helps.
I have this function that I use as a Custom Formatter in a JqGrid for ASP.NET WebForm
function formatLink(cellValue, options, rowObject) {
var res = cellValue.split(" - ");
var newLink = "";
var value = rowObject['Filter'];
var link = '<a class=\"clickCell\" href=\"#\" OnClick=\"CellClicked(\'value\')\">' + res[0] + '</a> - ' + res[1];
newLink = link.replace("value", value).replace("'", "\'");
return newLink;
}
the returned link does not work.
Here is an example:
<a onclick="CellClicked('vpd.esercizio = '2011-2012'')" href="#" class="clickCell">3</a>
As you can see, the var value contains string with quote. I will use its content to compose sql where condition.
Can you help me get things work?
Why don't you simply do
value.replace("'","\\'")
var link = '<a class="clickCell" href="#" OnClick="CellClicked('+value+')">' + res[0] + '</a> - ' + res[1];
?
With that replace you should be fine. I also removed some unnecesary back slashes because you can use double quotes inside simple quotes.
document.write(' "" '); returns ""