Get info a field of a page with javascript or jquery - javascript

i need to get an information of another page with javascript or jquery, to show that field on another page
the id of the field is "profile_field_13_1" and it shows the member status
I would like to get that "status" to put in an html page, how can i do that?
I already tried this:
jQuery(document).ready(function(){if(jQuery('#profile-advanced-right').length){
var status=jQuery('.middleline #profile_field_13_1 dd div:eq(0)').html();
<div class="status"><span id="cs">'+status+'</span></div>
');
}});

This is where you can use ajax.
var url = "mypage.html";
$.get(url,function(data){
var contentFromTheOtherPage = data;
//process it here
});

Related

Script to automate the passing credentials to next page

I have one URL which asks me to " click here to login" then the next page loads.
once the next page loads I need to pass the credential. tried below script but didn't work
document.getElementsByClassName('click_hereTo_login')[0].click();
$( document ).ready(function()
{ if document.location.href=="https://example.com/secondpage"
then {
document.getElementById('id').value="12322";
document.getElementById('password').value="4444";
document.getElementsByClassName('login')[0].click();
}})
First of all your if is not written correctly. It should be:
if (document.location.href=="https://example.com/secondpage"){
document.getElementById('id').value="12322";
document.getElementById('password').value="4444";
document.getElementsByClassName('login')[0].click();
}
The another problem here is, that your script runs from scratch on both pages. If you want to pass data from page to page without backend, you can use either query string or localstorage to store the data somewhere and then access it from another page.
let id = localStorage.getItem('id');
// get the input element
let idInput = document.getElementById('id');
// check if we are one second page
if(idInput) {
document.getElementById('id').value = id;
}
// Save id on login click
document.querySelector('.login').addEventListener('click', () => {
localStorage.setItem('id', 1234);
});

Get search box id present on website using javascript and C#

I am new to Javascript and C# and wanted to know how to get search box id of the search box present on website.
Kindly refer the approach i followed
var url = "example.com";
System.Net.WebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
{
<script type="text/javascript">
function searchAlive() {
var link_s = document.getElementById('search').value;
}
</script>
}
You can get the search box id by using Javascript/Jquery using some properties(css,text etc..) of the text box. include html so that we can help you better
If you want use JavaScript:
var _Searched = document.getElementById("SearchTextBoxID").val();
if you are using C#:
string _Searched = SearchTextBoxID.text;
you have to show your codes and your approach. what environment you are coding in and what language you are using ?
If you mean you don't know the "SearchBoxInput" ID and you want to get it, you can use other element such as the class name or tag name to get the element value. But as a programmer, you have to know and work with 'ID' when using backend codes like c# and javascript.
if you want to get the id:
var inputs = document.getElementByTagName("input");
for(i=0;i<=inputs.lenght;i++){
if(inputs[i].class == "searchClass"){ var id = input[i].id }
}

Is there any way to get login-user information in javascript (Django)?

I made a board application and implementing comment function using django framework.
I use jquery & ajax to GET and POST comments.
Now I want to add Edit and Delete function.
As you can see in image, there is edit and delete button. What I'm trying to do is to show those buttons only for the comment that current user had posted.
This is part of my ajax getting comments from my comments API.
$.ajax({
url: commentURL,
type: "GET",
success: function(data){
var numOfComments = data.length;
$(commentCountElement).html(numOfComments);
data.forEach(function(comment){
/* Get data from API results */
var commentUsername = comment.author_name;
var commentContent = comment.content;
var commentCreatedAt = comment.created_at;
var commentID = comment.id;
/* Create html li */
var listElement = $("<li>").addClass("comment-box");
/* Create div comment-meta */
var commentMetaDiv = $("<div>").addClass("comment-meta");
$(commentMetaDiv).append($("<span>").text(commentUsername));
$(commentMetaDiv).append($("<span>").addClass("date").text(commentCreatedAt));
/* Create div comment-content */
var commentContentDiv = $("<div>").addClass("comment-content");
$(commentContentDiv).text(commentContent);
/* Append */
$(listElement).append(commentMetaDiv);
$(listElement).append(commentContentDiv);
$(commentUnorderedListElement).append(listElement);
});
},
error: function(data){
console.log(textStatus);
return false;
}
});
Now, I have to compare current login user with comment.author_name so that I can add button depending on that result. But I have no idea how I can get current user's infos in js.
Need your help. thanks
I assume you are using session authentication.
You can do it in several ways.
First, you can add an extra field in your comments API by doing something like this:
comments['editable'] = request.user.user_name == comment.author_name
If it is True, user can edit the comment and vice versa. I believe this is a better way.
Second way is directly in your script get access to django user and compare there:
<script>
var editable = comment.author_name == {{ request.user.user_name }}
</script>
This is not a good approach. Because you are mixing django variable with jquery! If your script is in another file, it won't work at all.
So, I would go with the first way.
Hope it helps
In your templates:
<script>
var reply_name = {{ request.user.user_name }};
get_comment(repay_name) #call what you want using js
</script>

js refresh page while passing variable in yii

I am trying to refresh the page while pass an array from an onclick button.
Since I am using yii, posting isn't an option, and setting a session variable wasn't working. Any ideas would help. Thank you
<a style="width:100%;" onclick="my_picks_reset()" id="my_picks_reset">Reset</a>
<script>
$(function() {
/*set var picks = array of TBA and reset th my_picks div*/
$("#my_picks_reset").click(function() {
var my_picks = ['TBA','TBA','TBA','TBA','TBA','TBA','TBA','TBA'];
var url = document.URL;
$(location).attr('href',url,'my_picks',my_picks);
})
})
</script>
It seems from your comments that you're expecting POST request. Changing location of the page will give you GET request. So you have two options here:
1) Continue using location and read the the values from $_GET variable.
If you decide to use this option your need loop through my_picks array and construct the query string that would look like that:
?my_picks[]=arrayValue1&my_picks[]=arrayValue2... and do location.assign(currentLocation + composedQueryString)
2) The second better solution is to use $.ajax() to send values with post method.

Copy inside a variable the html of an external link

Here is my previous question: Filter new content to only display specific divs
I would like to get and copy some content from an external page inside a variable.
To perform it, I'm using the jquery.get function.
// This code is adding to the div the content
$('.result').html(data);
I would like to save the content inside a variable to be able to get only informations that I want
$.get('ajax/test.html', function(data) {
// Add to the <div class="result"> the html of data
$('.result').html(data);
// I would like some thing like this
var result = the html of data
});
Thanks for your help
The data variable IS the actual HTML you want...
$.get('ajax/test.html', function (data) {
var result = data;
alert(result);
});
Here is a working example
If you then want to use jquery on that HTML data, you must convert it to a JQuery object...
var htmlElement = $(result);
//can now do htmlElement.find(...
Here is an example that pulls the date from the BBC news website

Categories