How get values variable javascript from razor? - javascript

i have soure code :
$("#yes-modal").click(function (e) {
var img = $("#url").val();
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = *value variable img* })';
});
i want get value img, but i have error.
How can do it ? thank you.

Below syntax should work.
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = ' + img + '})';
Let me know if you face any issue.

The razor syntax is a serverside code. It is evaluated while rendering the page from server. But the javascript is evaluated at the client side (browser). So you have to split it like below code.
$("#yes-modal").click(function (e) {
var img = $("#url").val();
var loc = '#Url.Action("DownloadImages", "Hrd")';
var loc += '?id=' + img;
window.location = loc;
});

Related

Print Options Do Not Open

I have an html file in google script with JS, trying to print using the following code. It opens the document without the print options, I still have to do the cmd+P to print. Any idea, please?
function printBadge() {
var attendee = document.getElementById("info").value;
var fullName = "xxx";
var snb = "xxx";
var printFonts = "fonts";
var printStyle = "style";
var printArea = "badge info";
w = window.open();
w.document.write(printFonts + printStyle + printArea).html();
w.document.close();
w.focus();
w.print();
w.close();
return false;
}
The reason is this line w.document.write(printFonts + printStyle + printArea).html();. There is no function .html() associated to document.write and when you try to execute it, it fails there and so window gets stuck.
It needs to be w.document.write(printFonts + printStyle + printArea) without .html() function.
Hope it helps. Revert for any doubts.

Unable to call route through javascript function

I am trying to call a laravel route through javascript function. I have explored all the way on internet and over here but I am not finding up my solution. I referred to one answer over stack overflow about how to call route in javascript function but it didn't work for me. My route:
Route::get("edit_contact/{id}",'ContactController#edit');
Javascript function:
{
var url = '{{ route("edit_contact",":id") }}';
url = url.replace(':id', count);
document.location.href = url;
}
count is defined up in javascript which I need to pass in route.
Issue is this that it is continuously giving error..
Route [edit_contact] not defined.
All these routes are working fine when hitting through html. I have also tried to call up other routes as well through this, but none is working.
Name your route like this:
Route::get("edit_contact/{id}",'ContactController#edit')->name('edit_contact');
var routeUrl = route('posts.index');
var routeUrl = route('posts.show', {post: 1337});
// Returns results from /posts
return axios.get(route('posts.index'))
.then((response) => {
return response.data;
});
Try
let count=3
document.location.href = `edit_contact/${count}`;
try this piece. it should work:
Route::get("edit_contact/{id}",'ContactController#edit');
javascript code
<script type="text/javascript">
$("#valueid").change(function(e){
var id= e.target.value; // the id to be pass
//alert(id);
//checking the state of the domain if secured or not
if ("https:" == document.location.protocol) {
//alert('secured');
location.href="https://www.example.com/edit_contact/"+id;
} else {
//alert('not secured');
location.href="http://www.example.com/edit_contact/"+id;
}
});
</script>
In route file: (web.php)
Route::get("edit_contact/{id}",'ContactController#edit');
In js code:
Here I used count value 3 just for an example. You can use your actual value of count.
<script>
var count = 3;
var baseurl = window.location.protocol + "//" + window.location.host;
var url = baseurl + '/edit_contact/' + count;
document.location.href = url;
</script>

Redirect from master page to its content pages using jquery not working

I called this method on keyUp event of enter key
function PerformSearch(search_key) {
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
//GoToLocation(url);
//window.open(url);
//$(location).attr("href", url);
//window.location.replace(url);
window.location.href = url;
}
use like this
window.location = url;
For your reference read this
You have string concatenation errors in your url.
This:
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
Should be this:
var url = "<%=ResolveUrl('/Test/TestPage.aspx?type=search&search_key=')%>" + search_key;

Unable to receive parameter in query string when redirected to other page

Hi I am developing one application in java-script. I have two pages default.aspx and addnewitem.aspx. there is one html table in default.aspx and one button. When i click on button i want to redirect to addnewitem.aspx page. I have some parameters to send in query string. I am able to redirect to addnewitem.aspx but page not found error i am getting. I am not sure why i am getting page not found error. I am trying as below.
function getValues() {
var Title = "dfd";
var PrimarySkills = "fdfd";
var SecondarySkills = "dfdf";
var url = "http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=" + encodeURIComponent($(Title)) + "&PrimarySkills=" + encodeURIComponent($(PrimarySkills)) + "&SecondarySkills=" + encodeURIComponent($(SecondarySkills));
window.location.href = url;
}
I am checking querystring in addnewitem.aspx as below.
<script type="text/javascript">
var queryString = new Array();
$(function () {
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["Title"] != null && queryString["PrimarySkills"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Title:</b> " + queryString["Title"] + " <b>PrimarySkills:</b> " + queryString["PrimarySkills"] + " <b>SecondarySkills:</b> " + queryString["SecondarySkills"];
$("#lblData").html(data);
alert(data);
}
});
</script>
"http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=%5Bobject%20Object%5D&PrimarySkills=%5Bobject%20Object%5D&SecondarySkills=%5Bobject%20Object%5D"
I tried lot to fix this. May i know where i am doing wrong? Thanks for your help.
You should use the relative path in your url instead of hard coding the entire folder structure, which is probably incorrect since you are getting a 404. And you need to change the url every time you publish the site to the hosting enviroment when you hard code it like that.
So change
var url = "http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=...
into
var url = "/AddNewItem.aspx?Title=...
if both the pages are in the same folder. Should AddNewItem.aspx be located in the Pages folder, you have to add that folder of course: var url = "/Pages/AddNewItem.aspx?Title=...

Change HTML page's url & its content

I am trying to change the content of an html page when some action on the widget is taken.
Code:
function widget(newURL) {
var server_url = "127.0.0.1:8000";
var oldHTML = document.documentElement.innerHTML;
$.post(server_url + "/convert/",
{ input_html: oldHTML, convert: newURL },
function(response) {
var resp = JSON.stringify(response);
resp = resp.substring(1, resp.length - 1);
var jObj = JSON.parse(resp);
var win = window.open(newURL,'_self');
document.write(jObj.data);
});
}
With this code, though the HTML content gets changed but the HTML page's URL doesnot change. Can someone please suggest how can i change webpage URL as well as content both ?
Updated Code:
function widget(newURL) {
var server_url = "127.0.0.1:8000";
var oldHTML = document.documentElement.innerHTML;
$.post(server_url + "/convert/",
{ input_html: oldHTML, convert: newURL },
function(response) {
var resp = JSON.stringify(response);
resp = resp.substring(1, resp.length - 1);
var jObj = JSON.parse(resp);
window.history.pushState({"html":jObj.data},"", newURL);
document.documentElement.innerHTML = jObj.data;
});
}
In the updated Code, the URL changes as well as the content.
Thanks,
Try window.history :
window.history.pushState({"html":jObj.data,"pageTitle":'Hello'},"", newURL);
document.documentElement.innerHTML = jObj.data;
Take a look at this answer.
hope this helps.

Categories

Resources